This is a convenience method to load a resource as a stream. - Java Reflection

Java examples for Reflection:Class Loader

Description

This is a convenience method to load a resource as a stream.

Demo Code


import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main{
    public static void main(String[] argv) throws Exception{
        String name = "java2s.com";
        System.out.println(getResourceAsStream(name));
    }//  ww w .  j  a v a 2  s. c o m
    /**
     * This is a convenience method to load a resource as a stream.
     *
     * The algorithm used to find the resource is given in getResource()
     *
     * @param resourceName The name of the resource to load
     * @param callingClass The Class object of the calling object
     * @return  the InputStream of the specific resource on the 'resources' folder.
     */
    public static InputStream getResourceAsStream(String resourceName,
            Class<?> callingClass) {
        URL url = getResourceAsURL(resourceName, callingClass);
        try {
            return (url != null) ? url.openStream() : null;
        } catch (IOException e) {
            return null;
        }
    }
    public static InputStream getResourceAsStream(String name) {
        name = resolveName(name);
        ClassLoader cl = ClassLoader.getSystemClassLoader();
        if (cl == null) {
            // A system class.
            return ClassLoader.getSystemResourceAsStream(name);
        }
        return cl.getResourceAsStream(name);
    }
    /**
     * Load a given resource.
     *
     * This method will try to load the resource using the following methods (in order):
     * <ul>
     *  <li>From Thread.currentThread().getContextClassLoader()
     *  <li>From ClassLoaderUtil.class.getClassLoader()
     *  <li>callingClass.getClassLoader()
     * </ul>
     *
     * @param resourceName The name IllegalStateException("Unable to call ")of the resource to load
     * @param callingClass The Class object of the calling object
     * @return the URL of the specific resource on the 'resources' folder.
     */
    public static URL getResourceAsURL(String resourceName,
            Class<?> callingClass) {
        URL url = Thread.currentThread().getContextClassLoader()
                .getResource(resourceName);

        if (url == null) {
            url = ClassLoaderUtil.class.getClassLoader().getResource(
                    resourceName);
        }
        if (url == null) {
            ClassLoader cl = callingClass.getClassLoader();

            if (cl != null) {
                url = cl.getResource(resourceName);
            }
        }

        if ((url == null)
                && (resourceName != null)
                && ((resourceName.length() == 0) || (resourceName.charAt(0) != '/'))) {
            return getResourceAsURL('/' + resourceName, callingClass);
        }

        return url;
    }
    /**
     * Add a package name prefix if the name is not absolute Remove leading "/"
     * if name is absolute
     * @param name the {@link String} of the resources.
     * @return the {@link String} new name.
     */
    private static String resolveName(String name) {
        if (name == null)
            return null;
        if (!name.startsWith("/")) {
            Class<?> c = ClassLoaderUtil.class;
            while (c.isArray()) {
                c = c.getComponentType();
            }
            String baseName = c.getName();
            int index = baseName.lastIndexOf('.');
            if (index != -1) {
                name = baseName.substring(0, index).replace('.', '/') + "/"
                        + name;
            }
        } else {
            name = name.substring(1);
        }
        return name;
    }
}

Related Tutorials