Java Utililty Methods Resource Load

List of utility methods to do Resource Load

Description

The list of methods to do Resource Load are organized into topic(s).

Method

ListgetResourcesFromJarFile(File file, String resName)
get Resources From Jar File
List<String> retval = new ArrayList<String>();
try {
    ZipFile zf = new ZipFile(file);
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        String entry = ze.getName();
        if (entry.startsWith(resName)) {
...
CollectiongetResourcesFromJarFile(final File file, final Pattern pattern)
Gets the resources from jar file.
final ArrayList<String> retval = new ArrayList<String>();
ZipFile zf;
try {
    zf = new ZipFile(file);
} catch (final ZipException e) {
    throw new Error(e);
} catch (final IOException e) {
    throw new Error(e);
...
MapgetResourcesFromZip(final byte[] barContent)
get Resources From Zip
final Map<String, byte[]> resources = new HashMap<String, byte[]>();
final InputStream in = new ByteArrayInputStream(barContent);
final ZipInputStream zis = new ZipInputStream(in);
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
    if (!zipEntry.isDirectory()) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c;
...
longgetResourceSize(ClassLoader classLoader, String path)
get Resource Size
final InputStream is = classLoader.getResourceAsStream(path);
try {
    long n = 0;
    while (is.read() >= 0) {
        n++;
    return n;
} finally {
...
InputStreamgetResourceStream(Class clazz, String resourceName)
Returns a resource as a stream by checking: 1.
String cpResourceName = null;
if (!resourceName.startsWith("/")) {
    cpResourceName = "/" + resourceName;
} else {
    cpResourceName = resourceName;
InputStream is = clazz.getResourceAsStream(cpResourceName);
if (is == null) {
...
InputStreamgetResourceStream(final String resource)
get Resource Stream
return getClassLoader().getResourceAsStream(resource);
InputStreamgetResourceStream(String className, ClassLoader classLoader)
Gets the input stream from a class file.
return classLoader.getResourceAsStream(className.replace('.', '/') + ".class");
InputStreamgetResourceStream(String name)
Resources are those stored in the class path (e.g., src/main/resources).
ClassLoader loader = Thread.currentThread().getContextClassLoader();
return loader.getResourceAsStream(name);
InputStreamgetResourceStream(String pathToResource)
get Resource Stream
try {
    return Class.forName("org.smartfrog.tools.eclipse.core.CoreUtilities") 
            .getResourceAsStream(pathToResource);
} catch (ClassNotFoundException e) {
    return null;
InputStreamgetResourceStream(String resource, Class root)
Load a resource as a stream
String fullResourceName = resource;
InputStream istream = root.getResourceAsStream(fullResourceName);
if (istream == null && !resource.startsWith("/")) {
    fullResourceName = "/" + resource;
    istream = root.getResourceAsStream(fullResourceName);
    if (istream == null && !resource.startsWith("/")) {
        fullResourceName = "/" + resource;
        istream = root.getResourceAsStream(fullResourceName);
...