/** * <p> * Obtain a resource by name, throwing an exception if it is not present. * </p> * <p> * First the <em>context class loader</em> will be used. If this class loader is not * able to locate the resource, then the <em>class class loader</em> * ({@code ClassLoaderUtils.class.getClassLoader()}) will be used if it is different from * the thread context one. Last, the System class loader will be tried. * </p> * <p> * This method does never return {@code null}. * </p> * * @param resourceName the name of the resource to be obtained. * @return an input stream on the resource (null never returned). * @throws IOException if the resource could not be located. * * @since 3.0.3 * */ public static InputStream loadResourceAsStream(final String resourceName) throws IOException { final InputStream inputStream = findResourceAsStream(resourceName); if (inputStream != null) { return inputStream; } // No way to obtain that resource, so we must raise an IOException throw new IOException("Could not locate resource '" + resourceName + "' in the application's class path"); }
public Reader reader() throws IOException { final InputStream inputStream; if (this.optionalClassLoader != null) { inputStream = this.optionalClassLoader.getResourceAsStream(this.path); } else { inputStream = ClassLoaderUtils.findResourceAsStream(this.path); } if (inputStream == null) { throw new FileNotFoundException(String.format("ClassLoader resource \"%s\" could not be resolved", this.path)); } if (!StringUtils.isEmptyOrWhitespace(this.characterEncoding)) { return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream), this.characterEncoding)); } return new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream))); }