View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URL;
23  import java.nio.file.Files;
24  import java.util.Objects;
25  import java.util.Properties;
26  
27  /**
28   * Static methods to create Properties loaded from various sources.
29   *
30   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
31   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
32   */
33  public class PropertyUtils {
34  
35      public static Properties loadProperties(final URL url) throws IOException {
36          return loadProperties(Objects.requireNonNull(url, "url").openStream());
37      }
38  
39      public static Properties loadProperties(final File file) throws IOException {
40          return loadProperties(
41                  Files.newInputStream(Objects.requireNonNull(file, "file").toPath()));
42      }
43  
44      public static Properties loadProperties(final InputStream is) throws IOException {
45          final Properties properties = new Properties();
46  
47          // Make sure the properties stream is valid
48          if (is != null) {
49              try (InputStream in = is) {
50                  properties.load(in);
51              }
52          }
53  
54          return properties;
55      }
56  }