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.manydesigns.portofino.pageactions.PageActionLogic.java

public static String getScriptTemplate(Class<?> actionClass) {
    if (!PageAction.class.isAssignableFrom(actionClass)) {
        return null;
    }/*from   ww w .ja  v  a  2 s.  co  m*/
    ScriptTemplate scriptTemplate = actionClass.getAnnotation(ScriptTemplate.class);
    if (scriptTemplate != null) {
        String templateLocation = scriptTemplate.value();
        try {
            return IOUtils.toString(actionClass.getResourceAsStream(templateLocation));
        } catch (Exception e) {
            throw new Error(
                    "Can't load script template: " + templateLocation + " for class: " + actionClass.getName(),
                    e);
        }
    } else {
        String template = getScriptTemplate(actionClass.getSuperclass());
        if (template != null) {
            return template;
        } else {
            try {
                InputStream stream = PageActionLogic.class.getResourceAsStream(
                        "/com/manydesigns/portofino/pageactions/default_script_template.txt");
                return IOUtils.toString(stream);
            } catch (Exception e) {
                throw new Error("Can't load script template", e);
            }
        }
    }
}

From source file:ClassReader.java

/**
 * load the bytecode for a given class, by using the class's defining
 * classloader and assuming that for a class named P.C, the bytecodes are in
 * a resource named /P/C.class./*from www.  j av a 2  s  .c  o  m*/
 * 
 * @param c the class of interest
 * @return a byte array containing the bytecode
 * @throws IOException
 */
protected static byte[] getBytes(Class c) throws IOException {
    InputStream fin = c.getResourceAsStream('/' + c.getName().replace('.', '/') + ".class");
    if (fin == null) {
        throw new IOException();
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int actual;
        do {
            actual = fin.read(buf);
            if (actual > 0) {
                out.write(buf, 0, actual);
            }
        } while (actual > 0);
        return out.toByteArray();
    } finally {
        fin.close();
    }
}

From source file:org.bonitasoft.engine.test.BuildTestUtil.java

public static BarResource getContentAndBuildBarResource(final String name,
        final Class<? extends Connector> clazz) throws IOException {
    final InputStream stream = clazz.getResourceAsStream(name);
    assertNotNull(stream);/*from  w  w  w  .  ja v  a  2s .  c  o m*/
    final byte[] byteArray = IOUtils.toByteArray(stream);
    stream.close();
    return new BarResource(name, byteArray);
}

From source file:org.wrml.runtime.EngineConfiguration.java

/**
 * Load the EngineConfiguration from a named resource owned by the identified class.
 *//*from   w  w w.j av a2s.c  om*/
public final static EngineConfiguration load(final Class<?> resourceOwner, final String resourceName)
        throws IOException {

    final URL resource = resourceOwner.getResource(resourceName);
    LOGGER.trace("loading EngineConfiguration from  '{}' [{}]...", resourceName, resource);
    final EngineConfiguration result = EngineConfiguration
            .load(resourceOwner.getResourceAsStream(resourceName));
    LOGGER.debug("loaded EngineConfiguration from  '{}'", resource);
    return result;
}

From source file:org.apache.stanbol.client.StanbolClientTest.java

private static Properties loadProperties(final Class<?> loadingClass) throws IOException {
    final Properties result = new Properties();

    try (final InputStream propertyStream = loadingClass
            .getResourceAsStream(loadingClass.getSimpleName() + ".properties")) {
        result.load(propertyStream);/*from   w  ww  .  ja  v  a2s .  com*/
    }

    return result;
}

From source file:net.darkmist.alib.res.PkgRes.java

/**
 * Gets a InputStream for a class's package.
 * @param name The name of the resource.
 * @param cls The class to use for the package name
 * @return InputStream for the resource.
 * @throws NullPointerException if name or cls are null.
 *    ResourceException if the resource cannot be found.
 *//*from  w w  w  .  j  av  a  2s. c om*/
public static InputStream getFor(String name, Class<?> cls) {
    InputStream ret;

    if (cls == null)
        throw new NullPointerException("cls is null");
    if ((ret = cls.getResourceAsStream(getResourcePathFor(name, cls))) == null)
        throw new ResourceException("Unable to find resource for " + name + " and " + cls);
    return ret;
}

From source file:com.hp.ov.sdk.rest.client.FcSansManagedSanClientImplTest.java

@BeforeClass
public static void setupTest() throws IOException {
    Class<FcSansManagedSanClientImplTest> clazz = FcSansManagedSanClientImplTest.class;

    managedSan = IOUtils.toString(clazz.getResourceAsStream("ManagedSanResponse.json"), "UTF-8");
    managedSanList = IOUtils.toString(clazz.getResourceAsStream("ManagedSanListResponse.json"), "UTF-8");
    managedSanEndpoints = IOUtils.toString(clazz.getResourceAsStream("ManagedSanEndpointsResponse.json"),
            "UTF-8");
}

From source file:com.mastfrog.acteur.ClasspathResourcePage.java

protected static InputStream getStream(Path path, Class<?> type) {
    try {/* ww w  . j  a v  a 2  s .  c  o  m*/
        String name = URLDecoder.decode(path.getLastElement().toString(), "UTF-8");
        InputStream in = type.getResourceAsStream(name);
        return in;
    } catch (UnsupportedEncodingException ex) {
        throw new AssertionError(ex); //won't happen
    }
}

From source file:com.tesora.dve.common.PEFileUtils.java

/**
 * Helper function to load a file from the classpath relative to the
 * specified class and return it as an <code>InputStream</code>.
 * //from  w ww  . j  ava 2 s  .c  om
 * @param testClass
 *            <code>Class</code> of caller
 * @param fileName
 *            name of file to load
 * @return <code>InputStream</code> of file
 * @throws PEException
 *             if file cannot be located
 */
public static InputStream getResourceStream(Class<?> testClass, String fileName) throws PEException {

    InputStream is = testClass.getResourceAsStream(fileName);

    validateFileResource(fileName, is);

    return is;
}

From source file:org.openqa.selenium.server.browserlaunchers.LauncherUtils.java

public static InputStream getSeleniumResourceAsStream(String resourceFile) {
    Class clazz = ClassPathResource.class;
    InputStream input = clazz.getResourceAsStream(resourceFile);
    if (input == null) {
        try {/*from   w  w w .  j a v a 2 s  . c  o  m*/
            // This is hack for the OneJar version of Selenium-Server.
            // Examine the contents of the jar made by
            // https://svn.openqa.org/svn/selenium-rc/trunk/selenium-server-onejar/build.xml
            clazz = Class.forName("OneJar");
            input = clazz.getResourceAsStream(resourceFile);
        } catch (ClassNotFoundException e) {
        }
    }
    return input;
}