Java Resource Load getResourceAsStream(String name, Class aClass)

Here you can find the source of getResourceAsStream(String name, Class aClass)

Description

Return the input stream for the given name and class.

License

Apache License

Parameter

Parameter Description
name the name of the resource to return
aClass the class which class loader to use

Return

the input stream for the given name and class

Declaration

public static InputStream getResourceAsStream(String name, Class aClass) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.InputStream;

public class Main {
    /**/*from   w w  w . j  av  a2 s.  com*/
     * Return the input stream for the given name and class.
     *
     * @param name the name of the resource to return
     * @param aClass the class which class loader to use
     * @return the input stream for the given name and class
     */
    public static InputStream getResourceAsStream(String name, Class aClass) {
        if (isBlank(name)) {
            throw new IllegalArgumentException("Parameter name cannot be blank");
        }
        ;

        if (aClass == null) {
            throw new IllegalArgumentException("Parameter aClass is null");
        }

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        InputStream inputStream = classLoader.getResourceAsStream(name);
        if (inputStream == null) {
            inputStream = aClass.getResourceAsStream(name);
        }

        return inputStream;
    }

    /**
     * Checks if a String is whitespace, empty ("") or null.
     * <p/>
     *
     * @param str the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. getResourceAsStream(String filePath)
  2. getResourceAsStream(String name)
  3. getResourceAsStream(String name)
  4. getResourceAsStream(String name)
  5. getResourceAsStream(String name)
  6. getResourceAsStream(String name, Class clazz)
  7. getResourceAsStream(String name, Class clazz)
  8. getResourceAsStream(String name, Class clzz)
  9. getResourceAsStream(String path)