1 30 package org.pdfbox.util; 31 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.InputStream ; 35 import java.io.IOException ; 36 37 import java.util.Properties ; 38 39 45 public class ResourceLoader 46 { 47 48 51 private ResourceLoader() 52 { 53 } 55 56 65 public static InputStream loadResource( String resourceName ) throws IOException 66 { 67 ClassLoader loader = ResourceLoader.class.getClassLoader(); 68 69 InputStream is = null; 70 71 if( loader != null ) 72 { 73 is = loader.getResourceAsStream( resourceName ); 74 } 75 76 if( is == null ) 79 { 80 loader = ClassLoader.getSystemClassLoader(); 81 if( loader != null ) 82 { 83 is = loader.getResourceAsStream( resourceName ); 84 } 85 } 86 87 if( is == null ) 88 { 89 File f = new File ( resourceName ); 90 if( f.exists() ) 91 { 92 is = new FileInputStream ( f ); 93 } 94 } 95 96 return is; 97 } 98 99 108 public static Properties loadProperties( String resourceName ) throws IOException 109 { 110 Properties properties = null; 111 InputStream is = null; 112 try 113 { 114 is = loadResource( resourceName ); 115 if( is != null ) 116 { 117 properties = new Properties (); 118 properties.load( is ); 119 } 120 } 121 finally 122 { 123 if( is != null ) 124 { 125 is.close(); 126 } 127 } 128 return properties; 129 } 130 131 141 public static Properties loadProperties( String resourceName, Properties defaults ) throws IOException 142 { 143 InputStream is = null; 144 try 145 { 146 is = loadResource( resourceName ); 147 if( is != null ) 148 { 149 defaults.load( is ); 150 } 151 } 152 finally 153 { 154 if( is != null ) 155 { 156 is.close(); 157 } 158 } 159 return defaults; 160 } 161 } | Popular Tags |