List of utility methods to do Resource Load
InputStream | getResourceStreamFromClasspath(Class> clazz, String resourceName) Helper method for grabbing a file from the classpath and returning an InputStream InputStream is = clazz.getResourceAsStream(resourceName); if (is == null) { is = clazz.getClassLoader().getResourceAsStream(resourceName); if (is == null) throw new RuntimeException("resource not found in classpath: " + resourceName + " class is: " + clazz.getCanonicalName()); return is; ... |
InputStream | getResourceStreamInPackage(Class> clazz, String name) get Resource Stream In Package return clazz.getResourceAsStream("/" + clazz.getPackage().getName().replace(".", "/") + "/" + name); |
String | getResourceString(String filename) get Resource String String rootPath = "./Oracle/src/test/resources/co/digitaloracle/"; String string = new Scanner(new File(rootPath + filename)).useDelimiter("\\Z").next(); return string; |
URI | getResourceUri(final String packageToScan, final ClassLoader classLoader) get Resource Uri String folderToScan = packageToScan.replace(PACKAGE_SEPARATOR, RESOURCE_SEPARATOR); URL url = classLoader.getResource(folderToScan); if (url == null) { throw new IllegalArgumentException("No folder to scan found for package '" + packageToScan + "'."); try { if (url.getPath().contains(" ")) { url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath().replace(" ", "%20")); ... |
URL | getResourceUrl(final Class> aClass, final String aPath) get Resource Url final URL url = aClass.getResource(aPath); if (url == null) { throw new FileNotFoundException("Resource not found for " + aClass.toString() + ": " + aPath); } else { return url; |
URL | getResourceUrl(final String resourcePath) Gets the URL of a local resource. return ClassLoader.class.getResource(resourcePath); |
URL | getResourceUrl(String path, String extension, ClassLoader loader) get Resource Url if (path == null || loader == null) { return null; path = getResourcePath(path, extension); return loader.getResource(path); |
URL | getResourceURL(String resourcePath) Get an URL of a classpath-relative resource. final ClassLoader cl = Thread.currentThread().getContextClassLoader(); final URL url = cl.getResource(resourcePath); if (url == null) throw new IOException("Resource not found: " + resourcePath); return url; |
URL | getResourceUrl(String resourcePath) Returns the specified resource URL or null if not found. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return classLoader.getResource(resourcePath);
|
byte[] | getResourceUsingFileStreams(InputStream source) get Resource Using File Streams ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = source.read(buf)) > 0) { output.write(buf, 0, bytesRead); output.flush(); return output.toByteArray(); } catch (Exception ex) { return null; } finally { try { source.close(); output.close(); } catch (Exception ex) { |