Java Properties Load from InputStream loadProperties(InputStream stream)

Here you can find the source of loadProperties(InputStream stream)

Description

Reads a property list using the Properties#load(InputStream) method.

License

Open Source License

Parameter

Parameter Description
stream The stream to read from

Exception

Parameter Description
IOException propagated from the load method.

Return

The resulting map

Declaration

public static Map<String, String> loadProperties(InputStream stream) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2010 Cloudsmith Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w  ww.  jav  a2 s  .c o m
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/

import java.io.*;

import java.util.*;

public class Main {
    /**
     * Reads a property list using the {@link Properties#load(InputStream)} method. The
     * properties are stored in a map.
     * @param stream The stream to read from
     * @return The resulting map
     * @throws IOException propagated from the load method.
     */
    public static Map<String, String> loadProperties(InputStream stream) throws IOException {
        Properties properties = new Properties();
        properties.load(stream);
        return toMap(properties);
    }

    /**
     * Copies all elements from <code>properties</code> into a Map. The returned map might be unmodifiable
     * @param properties
     * @return The map containing all elements
     */
    public static Map<String, String> toMap(Properties properties) {
        if (properties == null || properties.isEmpty())
            return emptyMap();

        Map<String, String> props = new HashMap<String, String>(properties.size());
        putAll(properties, props);
        return props;
    }

    /**
     * The emptyMap() method was introduced in Java 1.5 so we need this here to be able to
     * down compile to 1.4.
     * @param <K> The type of the map keys
     * @param <V> The type of the map values
     * @return An empty set
     */
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> emptyMap() {
        return Collections.EMPTY_MAP;
    }

    /**
     * Copies all elements from <code>properties</code> into the given <code>result</code>.
     * @param properties
     * @param result
     */
    public static void putAll(Properties properties, Map<String, String> result) {
        for (Enumeration<Object> keys = properties.keys(); keys.hasMoreElements();) {
            String key = (String) keys.nextElement();
            result.put(key, properties.getProperty(key));
        }
    }
}

Related

  1. loadProperties(InputStream input)
  2. loadProperties(InputStream inputStream)
  3. loadProperties(InputStream inputStream)
  4. loadProperties(InputStream inputStream)
  5. loadProperties(InputStream inputStream, Properties result)
  6. loadProperties(Properties props, InputStream inputStream)
  7. loadPropertiesFileFromStream(InputStream stream)
  8. loadPropertiesFromStream(InputStream inputStream)
  9. loadPropertiesInputStreamToMap(InputStream propertiesInputStream)