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.playonlinux.qt.common.ResourceHelper.java

/**
 * Generate a QIODevice for the resource with the given name of the given class
 * @param c Class that should be used for locating the resource
 * @param resourcePath relative path of the resource
 * @return A QBuffer filled with the data from the resource
 *///from w  w w  .ja  va  2s . co  m
public static QBuffer getDeviceFromResource(Class<?> c, String resourcePath) {
    return getDeviceFromStream(c.getResourceAsStream(resourcePath));
}

From source file:uk.ac.ebi.atlas.utils.Files.java

public static String readTextFileFromClasspath(Class aClass, String fileName) throws IllegalStateException {

    try {/* w  w  w .j  a  v a 2 s .  c om*/

        StringWriter writer = new StringWriter();
        IOUtils.copy(aClass.getResourceAsStream(fileName), writer, "UTF-8");
        return writer.toString();

    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

}

From source file:org.springmodules.validation.valang.javascript.AbstractValangJavaScriptTranslator.java

/**
 * Returns a <code>Reader</code> for accessing the JavaScript codebase used by the translated validation rules.
 *///from w w  w. ja  v  a2s .  c  om
public static Reader getCodebaseReader() {
    Class clazz = AbstractValangJavaScriptTranslator.class;
    InputStream resourceAsStream = clazz.getResourceAsStream(VALANG_CODEBASE_JS);

    Assert.notNull(resourceAsStream, "Valang Codebase not found!");
    return new InputStreamReader(resourceAsStream);
}

From source file:org.sonar.api.checks.profiles.CheckProfileXmlMarshaller.java

public static CheckProfile fromXmlInClasspath(String pathToXml, Class clazz) {
    Reader reader = new InputStreamReader(clazz.getResourceAsStream(pathToXml));
    try {/*  ww w .ja v  a2 s  .  co m*/
        return fromXml(reader);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:jp.ac.u.tokyo.m.resource.ResourceLoadUtil.java

/**
 * This method read Ini in the same package. <br>
 * If the file does not exist, throws exception. <br>
 * <br>//from www  . j av a2 s .co  m
 * aFileName ????? Ini ???? <br>
 * ?????? <br>
 */
public static Ini loadNecessaryPackagePrivateIni(Class<?> aClass, String aFileName) {
    return loadNecessaryIni(aClass, aFileName, aClass.getResourceAsStream(aFileName));
}

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

protected static InputStream getArchiveResource(Class<? extends ArchiveFactoryTest> clazz,
        String archiveResource) {
    return clazz.getResourceAsStream(archiveResource);
}

From source file:jp.ac.u.tokyo.m.resource.ResourceLoadUtil.java

/**
 * This method read Ini in the same package. <br>
 * If the file does not exist, logging WARN. <br>
 * <br>/* w  ww.  ja v a2s .c o  m*/
 * aFileName ????? Ini ???? <br>
 * ???????? <br>
 */
public static Ini loadUnnecessaryPackagePrivateIni(Class<?> aClass, String aFileName) {
    try {
        return loadIni(aClass, aFileName, aClass.getResourceAsStream(aFileName));
    } catch (NullPointerException e) {
        LogFactory.getLog(aClass).warn("not found : " + aClass.getPackage() + "." + aFileName);
        return new Ini();
    }
}

From source file:net.nicholaswilliams.java.teamcity.plugin.buildNumber.PluginFileUtils.java

@SuppressWarnings("unchecked")
public static List<String> readLines(Class<?> relativeClass, String resourceName) {
    InputStream inputStream = null;
    try {/*from   w w  w . ja  v a 2s .  co  m*/
        inputStream = relativeClass.getResourceAsStream(resourceName);
        if (inputStream == null) {
            throw new RuntimeException("No resource with name [" + resourceName + "] found relative to class ["
                    + relativeClass.getName() + "].");
        }

        return IOUtils.readLines(inputStream);
    } catch (IOException e) {
        throw new RuntimeException("Failed to read lines from resource [" + resourceName
                + "] relative to class [" + relativeClass.getName() + "].");
    } finally {
        try {
            if (inputStream != null)
                inputStream.close();
        } catch (IOException ignore) {
        }
    }
}

From source file:org.vafer.jdependency.utils.DependencyUtils.java

public static Set<String> getDependenciesOfClass(final Class<?> pClass) throws IOException {
    final String resource = "/" + pClass.getName().replace('.', '/') + ".class";
    return getDependenciesOfClass(pClass.getResourceAsStream(resource));
}

From source file:Main.java

public static String readFileToString(final Class contextClass, final String streamIdentifier)
        throws IOException {

    InputStreamReader inputStreamReader = null;
    try {/*from   ww  w. ja v  a  2  s  .com*/
        inputStreamReader = new InputStreamReader(contextClass.getResourceAsStream(streamIdentifier));
        return CharStreams.toString(inputStreamReader);
    } catch (IOException e) {
        throw new IOException();
    } finally {
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
    }
}