Java Resource Load getResourceAsStream(Class clazz, String resource, boolean checkThreadContextFirst)

Here you can find the source of getResourceAsStream(Class clazz, String resource, boolean checkThreadContextFirst)

Description

Get an input stream from a named resource.

License

Apache License

Parameter

Parameter Description
clazz class to use in the lookups
resource resource string to look for
checkThreadContextFirst check the thread context first?

Return

input stream if found, or null

Declaration

public static InputStream getResourceAsStream(Class clazz,
        String resource, boolean checkThreadContextFirst) 

Method Source Code

//package com.java2s;
/*//from   w ww  .java  2s . com
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.InputStream;

public class Main {
    /** hash table of class loaders */
    private static java.util.Hashtable classloaders = new java.util.Hashtable();

    /**
     * Get an input stream from a named resource.
     * Tries
     * <ol>
     * <li>the classloader that loaded "clazz" first,
     * <li>the system classloader
     * <li>the class "clazz" itself
     * </ol>
     * @param clazz class to use in the lookups
     * @param resource resource string to look for
     * @param checkThreadContextFirst check the thread context first?
     * @return input stream if found, or null
     */
    public static InputStream getResourceAsStream(Class clazz,
            String resource, boolean checkThreadContextFirst) {
        InputStream myInputStream = null;

        if (checkThreadContextFirst
                && Thread.currentThread().getContextClassLoader() != null) {
            // try the context class loader.
            myInputStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(resource);
        }
        if (myInputStream == null) {
            // if not found in context class loader fall back to default
            myInputStream = getResourceAsStream(clazz, resource);
        }
        return myInputStream;
    }

    /**
     * Get an input stream from a named resource.
     * Tries
     * <ol>
     * <li>the classloader that loaded "clazz" first,
     * <li>the system classloader
     * <li>the class "clazz" itself
     * </ol>
     * @param clazz class to use in the lookups
     * @param resource resource string to look for
     * @return input stream if found, or null
     */
    public static InputStream getResourceAsStream(Class clazz,
            String resource) {
        InputStream myInputStream = null;

        if (clazz.getClassLoader() != null) {
            // Try the class loader that loaded this class.
            myInputStream = clazz.getClassLoader().getResourceAsStream(
                    resource);
        } else {
            // Try the system class loader.
            myInputStream = ClassLoader.getSystemClassLoader()
                    .getResourceAsStream(resource);
        }
        if (myInputStream == null
                && Thread.currentThread().getContextClassLoader() != null) {
            // try the context class loader.
            myInputStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(resource);
        }
        if (myInputStream == null) {
            // if not found in classpath fall back to default
            myInputStream = clazz.getResourceAsStream(resource);
        }
        return myInputStream;
    }

    /**
     * Obtain the ClassLoader (if any) associated with the given
     * className.
     *
     * @param className the name of a class
     * @return class loader
     */
    public static ClassLoader getClassLoader(String className) {
        if (className == null) {
            return null;
        }
        return (ClassLoader) classloaders.get(className);
    }
}

Related

  1. getResourceAsReader(String relativeName, Class clazz, String enc)
  2. getResourceAsStream(Class baseclass, String name)
  3. getResourceAsStream(Class claz, String name)
  4. getResourceAsStream(Class clazz, String name)
  5. getResourceAsStream(Class clazz, String resource)
  6. getResourceAsStream(Class clazz, String fileName)
  7. getResourceAsStream(Class clazz, String resource)
  8. getResourceAsStream(Class clazz, String resourceName)
  9. getResourceAsStream(Class testClass, String resource)