Java Resource Load getResourceAsStream(String path, ClassLoader loader)

Here you can find the source of getResourceAsStream(String path, ClassLoader loader)

Description

get Resource As Stream

License

Open Source License

Declaration

protected static InputStream getResourceAsStream(String path, ClassLoader loader) 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    protected static InputStream getResourceAsStream(String path, ClassLoader loader) {
        // make sure the class loader isn't null
        if (loader == null) {
            //             log.debug("No loader for get resource request", "path", path);
            return null;
        }/*from   w  w  w.  j  a  v a 2s  .  c  o m*/
        // try the path as is
        InputStream in = loader.getResourceAsStream(path);
        if (in != null) {
            return in;
        }
        // try toggling the leading slash
        return loader.getResourceAsStream(togglePath(path));
    }

    protected static String togglePath(String path) {
        if (path.startsWith("/")) {
            return path.substring(1);
        } else {
            return "/" + path;
        }
    }
}

Related

  1. getResourceAsStream(String name, Class clazz)
  2. getResourceAsStream(String name, Class clzz)
  3. getResourceAsStream(String path)
  4. getResourceAsStream(String path, Class caller)
  5. getResourceAsStream(String path, ClassLoader cl)
  6. getResourceAsStream(String relativeName, Class clazz)
  7. getResourceAsStream(String res)
  8. getResourceAsStream(String resName, Object obj)
  9. getResourceAsStream(String resource, ClassLoader classLoader)