Example usage for java.lang ClassLoader getResourceAsStream

List of usage examples for java.lang ClassLoader getResourceAsStream

Introduction

In this page you can find the example usage for java.lang ClassLoader getResourceAsStream.

Prototype

public InputStream getResourceAsStream(String name) 

Source Link

Document

Returns an input stream for reading the specified resource.

Usage

From source file:com.mayalogy.mayu.io.LocalDataManager.java

public static Object readFromJarAndDeserialize(String resourceName) throws ClassNotFoundException, IOException {
    InputStream is = Class.class.getResourceAsStream("/" + resourceName);
    if (is == null) { //Used for when jar is running in a servlet
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        is = classLoader.getResourceAsStream(resourceName);
    }/*from w  ww  .j  a  va2 s  .c o  m*/
    ObjectInputStream ois = new ObjectInputStream(is);
    Object oRead = ois.readObject();
    ois.close();
    return oRead;
}

From source file:com.jiminger.util.LibraryLoader.java

private static InputStream getInputStreamFromClassLoader(ClassLoader loader, String resource) {
    InputStream is = loader.getResourceAsStream(resource);
    if (is == null)
        is = loader.getResourceAsStream("/" + resource);

    return is;// w ww . ja v  a 2  s  .  co  m
}

From source file:Main.java

/**
 * Loads an image from within a jar or from the file system.
 * //from w w  w  . j  a  va 2s .c om
 * @param fileName the image path. 
 * @param packageClass a class within the package where the image can be found
 * @return an initialised image-icon or null, when no image could be found.
 */
public static Image loadIcon(String fileName, Class packageClass) {
    try {
        ClassLoader classLoader = packageClass.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(fileName);
        if (is == null) {
            File file = new File(fileName);
            if (file.exists()) {
                is = new FileInputStream(file);
            } else {
                return null;
            }
        }
        return ImageIO.read(is);
    } catch (IOException e) {
        System.err.println("Unable to load image [" + fileName + "]");
        e.printStackTrace();
        return null;
    }
}

From source file:org.apache.shindig.common.util.ResourceLoader.java

/**
 * @param resource/*  www . jav  a2  s.  c  o  m*/
 * @return An input stream for the given named resource
 * @throws FileNotFoundException
 */
public static InputStream openResource(String resource) throws IOException {
    ClassLoader cl = ResourceLoader.class.getClassLoader();
    InputStream is = cl.getResourceAsStream(resource.trim());
    if (is == null) {
        throw new FileNotFoundException("Can not locate resource: " + resource);
    }
    return is;
}

From source file:fi.johannes.kata.ocr.utils.ResourceGetter.java

public static Reader getReader(String fileName) {
    ClassLoader classLoader = ResourceGetter.class.getClassLoader();
    Reader r = new InputStreamReader(classLoader.getResourceAsStream(fileName));
    return r;/* w ww  . ja  v a2  s.c  o m*/
}

From source file:basex.BindContext.java

public static String getFile(String fileName) {

    String result = "";

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {/*from   w ww .  j  a v  a 2s  .co  m*/
        result = IOUtils.toString(classLoader.getResourceAsStream(fileName));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;

}

From source file:com.dotmarketing.util.UpdateUtil.java

/**
 * Loads the update.properties file//from  w w  w  .j a v  a  2  s.c  o m
 *
 * @return the update.properties properties
 */
private static Properties loadUpdateProperties() {

    Properties props = new Properties();

    ClassLoader cl = UpdateUtil.class.getClassLoader();
    InputStream is = cl.getResourceAsStream(Constants.PROPERTIES_UPDATE_FILE_LOCATION);
    try {
        props.load(is);
    } catch (IOException e) {
        Logger.debug(UpdateUtil.class, "IOException: " + e.getMessage(), e);
    }

    return props;
}

From source file:de.marza.firstspirit.modules.logging.fsm.FsmIT.java

/**
 * Sets up before.//from  ww  w .  jav a 2 s  .co m
 *
 * @throws Exception the exception
 */
@BeforeClass
public static void setUpBefore() throws Exception {
    pomProperties = new Properties();
    final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
    final InputStream inputStream = systemClassLoader.getResourceAsStream("moduleTest.properties");
    pomProperties.load(inputStream);
}

From source file:br.com.riselabs.cotonet.model.db.Database.java

public static Connection getConnection() {
    Thread currentThread = Thread.currentThread();
    ClassLoader classloader = currentThread.getContextClassLoader();
    InputStream input = classloader.getResourceAsStream("db.properties");
    Properties prop = new Properties();
    try {/*  w w  w .  ja v  a2s .  c o  m*/
        if (input != null) {
            prop.load(input);
            input.close();
        } else {
            Logger.logStackTrace(new FileNotFoundException("The file 'db.properties was not found."));
        }
    } catch (IOException e) {
        Logger.logStackTrace(e);
    }
    String db = prop.getProperty("database.name");
    String user = prop.getProperty("database.user");
    String pass = prop.getProperty("database.password");
    return getConnection(db, user, pass);
}

From source file:com.bigfatgun.fixjures.json.JSONSource.java

public static FixtureSource newJsonResource(final ClassLoader clsLoader, final String resourceName) {
    final InputStream input = clsLoader.getResourceAsStream(resourceName);
    if (input == null) {
        throw new FixtureException("Unable to locate resource: " + resourceName);
    } else {//  w  w  w. j  a  v  a2 s.  c o  m
        return new JSONSource(input);
    }
}