Here you can find the source of getResourceAsStream(final String resourceName, final Class caller)
Parameter | Description |
---|---|
resourceName | The name of the class to load. |
caller | The class of the caller. |
public static InputStream getResourceAsStream(final String resourceName, final Class caller)
//package com.java2s; /*/*from w w w. j av a 2s . c o m*/ Milyn - Copyright (C) 2006 - 2010 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (version 2.1) as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details: http://www.gnu.org/licenses/lgpl.txt */ import java.io.InputStream; public class Main { /** * Get the specified resource as a stream. * * @param resourceName * The name of the class to load. * @param caller * The class of the caller. * @return The input stream for the resource or null if not found. */ public static InputStream getResourceAsStream(final String resourceName, final Class caller) { final String resource; if (!resourceName.startsWith("/")) { final Package callerPackage = caller.getPackage(); if (callerPackage != null) { resource = callerPackage.getName().replace('.', '/') + '/' + resourceName; } else { resource = resourceName; } return getResourceAsStream(resource, caller.getClassLoader()); } else { return getResourceAsStream(resourceName, caller.getClassLoader()); } } /** * Get the specified resource as a stream. * * @param resourceName The name of the class to load. * @param classLoader The ClassLoader to use, if the resource is not located via the * Thread context ClassLoader. * @return The input stream for the resource or null if not found. */ public static InputStream getResourceAsStream(final String resourceName, final ClassLoader classLoader) { final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader(); final String resource; if (resourceName.startsWith("/")) { resource = resourceName.substring(1); } else { resource = resourceName; } if (threadClassLoader != null) { final InputStream is = threadClassLoader.getResourceAsStream(resource); if (is != null) { return is; } } if (classLoader != null) { final InputStream is = classLoader.getResourceAsStream(resource); if (is != null) { return is; } } return ClassLoader.getSystemResourceAsStream(resource); } }