/** * Convenient method to retrieve named resource as reader. Uses {@link #getResourceAsStream(String)} and return the * stream wrapped into a reader. * * @param name resource name. * @return reader for named resource. * @throws UnsupportedEncodingException if Java run-time does not support UTF-8 encoding. * @throws NoSuchBeingException if resource not found. */ public static Reader getResourceAsReader(String name) throws UnsupportedEncodingException { return new InputStreamReader(getResourceAsStream(name), "UTF-8"); }
/** * Retrieve binary resource as array of bytes. Uses {@link #getResourceAsStream(String)} to read binary resource and * return bytes array. * * @param name resource name. * @return binary resource content as array of bytes. * @throws NoSuchBeingException if resource not found. * @throws IOException if resource reading fails. */ public static byte[] getResourceAsBytes(String name) throws IOException { InputStream is = getResourceAsStream(name); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { byte[] buffer = new byte[4096]; int length = 0; while((length = is.read(buffer)) != -1) { os.write(buffer, 0, length); } } finally { Files.close(is); Files.close(os); } return os.toByteArray(); }
InputStream stream = getResourceAsStream(name, new ClassLoader[]
/** * Construct application configuration instance with information from given servlet context. * * @param context Servlet context bound to application, * @param properties context properties defining variables to inject on descriptors. * @throws ConfigException if application descriptor is not well formed or invalid. */ public TinyConfigBuilder(ServletContext context, Properties properties) throws ConfigException { config = new Config(getAppName(context.getContextPath())); File contextDir = new File(context.getRealPath("")); File webinfDir = new File(contextDir, "WEB-INF"); Loader loader = new Loader(config, properties); // first load library descriptor loadXML(Classes.getResourceAsStream("lib-descriptor.xml"), loader); // load application specific configuration document(s) File file = new File(webinfDir, "app.xml"); try { loadXML(new FileInputStream(file), loader); } catch (FileNotFoundException e) { log.debug("Application |%s| has no descriptor. No application specific configuration.", config.getName()); new ConfigException(e); } }
public TestConfigBuilder(InputStream config, Properties properties) throws ConfigException { super("test-app"); Loader loader = new Loader(build(), properties); loadXML(Classes.getResourceAsStream("lib-descriptor.xml"), loader); if (config != null) { loadXML(config, loader); } } }