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 Resource As Stream

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;

import java.io.InputStream;

public class Main {
    public static InputStream getResourceAsStream(final String resourceName, final Class<?> caller) {
        final String resource;
        if (resourceName.startsWith("/")) {
            resource = resourceName.substring(1);
        } else {/* www  . j  a v a  2  s . c  o m*/
            final Package callerPackage = caller.getPackage();
            if (callerPackage != null) {
                resource = callerPackage.getName().replace('.', '/') + '/' + resourceName;
            } else {
                resource = resourceName;
            }
        }
        final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
        if (threadClassLoader != null) {
            final InputStream is = threadClassLoader.getResourceAsStream(resource);
            if (is != null) {
                return is;
            }
        }

        final ClassLoader classLoader = caller.getClassLoader();
        if (classLoader != null) {
            final InputStream is = classLoader.getResourceAsStream(resource);
            if (is != null) {
                return is;
            }
        }

        return ClassLoader.getSystemResourceAsStream(resource);
    }
}

Related

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