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