Here you can find the source of getResourceAsStream(Class clazz, String resource, boolean checkThreadContextFirst)
Parameter | Description |
---|---|
clazz | class to use in the lookups |
resource | resource string to look for |
checkThreadContextFirst | check the thread context first? |
public static InputStream getResourceAsStream(Class clazz, String resource, boolean checkThreadContextFirst)
//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); } }