Java Resource Load getResourceAsStream(final String resourceName, final Class caller)

Here you can find the source of getResourceAsStream(final String resourceName, final Class caller)

Description

Get the specified resource as a stream.

License

Open Source License

Parameter

Parameter Description
resourceName The name of the class to load.
caller The class of the caller.

Return

The input stream for the resource or null if not found.

Declaration

public static InputStream getResourceAsStream(final String resourceName, final Class caller) 

Method Source Code

//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);
    }
}

Related

  1. getResourceAsStream(Class clazz, String resource)
  2. getResourceAsStream(Class clazz, String resourceName)
  3. getResourceAsStream(Class testClass, String resource)
  4. getResourceAsStream(final String fileName, ClassLoader classLoader)
  5. getResourceAsStream(final String name)
  6. getResourceAsStream(final String resourceName, final Class caller)
  7. getResourceAsStream(Object context, String resourceName)
  8. getResourceAsStream(Object o, String resource)
  9. getResourceAsStream(String filename)