Example usage for java.lang Class getResourceAsStream

List of usage examples for java.lang Class getResourceAsStream

Introduction

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

Prototype

@CallerSensitive
public InputStream getResourceAsStream(String name) 

Source Link

Document

Finds a resource with a given name.

Usage

From source file:com.controlj.addon.weather.service.WeatherServiceUIBase.java

protected void copyHTMLTemplate(Class clazz, String templatename, Writer out) {
    InputStream stream = clazz.getResourceAsStream(templatename);
    try {//from www.  ja va  2  s.  c om
        IOUtils.copy(stream, out);
    } catch (IOException e) {
        Logging.println("Error reading template html file", e);
    }
}

From source file:fr.duminy.jbackup.core.archive.ArchiveFactoryTest.java

private InputStream getResourceAsStream(String resource, boolean specificClass) {
    Class<?> clazz = specificClass ? getClass() : ArchiveFactoryTest.class;
    return clazz.getResourceAsStream(resource);
}

From source file:org.pentaho.platform.pdi.AgileBiPluginResourceLoader.java

@Override
public InputStream getResourceAsStream(Class<?> aClass, String s) {
    String symbolicName = getSymbolicName(aClass);
    return aClass.getResourceAsStream("/" + symbolicName + "/" + s);
}

From source file:edu.indiana.lib.osid.base.loader.OsidLoader.java

/**
 * Get an InputStream for a particular file name - first check the sakai.home area and then
 * revert to the classpath.//from   w w w .j  a va  2s .c  o m
*
* This is a utility method used several places.
 */
public static java.io.InputStream getConfigStream(String fileName, Class curClass) {
    String sakaiHome = System.getProperty("sakai.home");
    String filePath = sakaiHome + fileName;

    try {
        java.io.File f = new java.io.File(filePath);
        if (f.exists()) {
            return new java.io.FileInputStream(f);
        }
    } catch (Throwable t) {
        // Not found in the sakai.home area
    }

    if (curClass == null)
        return null;

    // If there is a class context, load from the class context...
    java.io.InputStream istream = null;

    // Load from the class loader
    istream = curClass.getClassLoader().getResourceAsStream(fileName);
    if (istream != null)
        return istream;

    // Load from the class relative
    istream = curClass.getResourceAsStream(fileName);
    if (istream != null)
        return istream;

    // Loading from the class at the root
    istream = curClass.getResourceAsStream("/" + fileName);
    return istream;
}

From source file:cherry.foundation.sql.SqlLoaderImpl.java

@Override
public Map<String, String> load(Class<?> klass) throws IOException {
    String name = klass.getSimpleName() + extension;
    try (InputStream in = klass.getResourceAsStream(name)) {
        return load(in);
    }//from w w  w. ja v a2s .c  om
}

From source file:com.liferay.sync.engine.BaseTestCase.java

protected final InputStream getInputStream(String fileName) {
    Class<?> clazz = getClass();

    return clazz.getResourceAsStream(fileName);
}

From source file:com.sonicle.webtop.core.versioning.SqlScript.java

public SqlScript(Class clazz, String resourceName) throws IOException, UnsupportedOperationException {
    InputStream is = null;//from  w ww  . ja v a  2s. co m

    try {
        is = clazz.getResourceAsStream(resourceName);
        if (is == null)
            throw new ResourceNotFoundException("Null InputStream!");
        readFile(new InputStreamReader(is, "ISO-8859-15"));
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.lnicholls.galleon.util.Tools.java

public static Image getResourceAsImage(Class theClass, String resource) {
    try {/*  w  ww  . j ava  2s . c  o m*/
        if (!resource.startsWith("/"))
            resource = "/" + resource;
        InputStream is = theClass.getResourceAsStream(resource);
        BufferedInputStream bis = new BufferedInputStream(is);
        if (is != null) {
            byte[] byBuf = new byte[is.available()];
            int byteRead = bis.read(byBuf, 0, is.available());
            Image img = Toolkit.getDefaultToolkit().createImage(byBuf);
            if (img != null) {
                img.getWidth(null);
                is.close();
                return img;
            }
            is.close();
        }
    } catch (Exception ex) {
        Tools.logException(Tools.class, ex, "Could not load resource: " + resource);
    }
    return null;
}

From source file:org.sakaibrary.osid.loader.OsidLoader.java

/**
 * Get an InputStream for a particular file name - first check the sakai.home area and then 
 * revert to the classpath.//www. j  ava2  s.c o  m
 *
 * This is a utility method used several places.
 */
public static java.io.InputStream getConfigStream(String fileName, Class curClass) {
    String dataFolder = "/data/";
    String filePath = dataFolder + fileName;

    try {
        java.io.File f = new java.io.File(filePath);
        if (f.exists()) {
            return new java.io.FileInputStream(f);
        }
    } catch (Throwable t) {
        // Not found in the sakai.home area
    }

    if (curClass == null)
        return null;

    // If there is a class context, load from the class context...
    java.io.InputStream istream = null;

    // Load from the class loader
    istream = curClass.getClassLoader().getResourceAsStream(fileName);
    if (istream != null)
        return istream;

    // Load from the class relative
    istream = curClass.getResourceAsStream(fileName);
    if (istream != null)
        return istream;

    // Loading from the class at the data root
    istream = curClass.getResourceAsStream("/data/" + fileName);
    return istream;
}

From source file:com.thistech.spotlink.AbstractSpotlinkTest.java

protected Object unmarshal(Class clazz, String filename) {
    try {//from   w w  w.  java 2 s .c o  m

        Object object = this.jaxbContext.createUnmarshaller().unmarshal(
                new StreamSource(new InputStreamReader(clazz.getResourceAsStream(filename), "UTF-8")));
        if (object instanceof JAXBElement) {
            return ((JAXBElement) object).getValue();
        }
        return object;
    } catch (Exception e) {
        return null;
    }
}