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:objective.taskboard.utils.IOUtilities.java

public static byte[] resourceToBytes(Class<?> clazz, String path) {
    InputStream inputStream = clazz.getResourceAsStream(path);
    if (inputStream == null)
        return null;
    try {/*from ww w.j a v a  2 s. c  o m*/
        return IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.hp.ov.sdk.dto.ExtraStorageVolumeTest.java

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

    extraAccessList = IOUtils.toString(clazz.getResourceAsStream("ExtraAccessList.json"), "UTF-8");
}

From source file:com.gooddata.util.ResourceUtils.java

private static InputStream readFromResource(String resourcePath, Class<?> testClass) {
    notEmpty(resourcePath, "resourcePath");
    notNull(testClass, "testClass");

    return testClass.getResourceAsStream(resourcePath);
}

From source file:com.confighub.api.common.Files.java

/**
 * Read local file from resources folder with a path relative to the test.
 *
 * @param testClass//from  ww  w .jav  a  2  s .com
 * @param file
 * @return String content of the file
 */
public static String readLocalFile(Class testClass, String file) {
    try {
        return IOUtils.toString(testClass.getResourceAsStream(file), "UTF-8");
    } catch (IOException e) {
        fail(e.getMessage());
        return "";
    }
}

From source file:Main.java

/** read a resource as a String
 * /*from  w  w w  .  j av  a 2s .  com*/
 * @param clazz
 * @param name
 * @return
 * @throws IOException
 */
public static String readResourceAsString(Class clazz, String name) throws IOException {
    StringBuffer fileData = new StringBuffer(1000);
    InputStreamReader reader = new InputStreamReader(clazz.getResourceAsStream(name));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString().replace("\r\n", "\n");
}

From source file:com.github.aelstad.keccakj.fips202.KeccakDigestTestUtils.java

public static InputStream getResourceStreamInPackage(Class<?> clazz, String name) {
    return clazz.getResourceAsStream("/" + clazz.getPackage().getName().replace(".", "/") + "/" + name);
}

From source file:com.synopsys.integration.util.ResourceUtil.java

public static String getResourceAsString(final Class<?> clazz, final String resourceName,
        final Charset encoding) throws IOException {
    final InputStream inputStream = clazz.getResourceAsStream(resourceName);
    if (inputStream != null) {
        return IOUtils.toString(inputStream, encoding);
    }//from   w w w.jav  a2 s .com
    return null;
}

From source file:com.xinlv.test.PortalTestUtil.java

public static String[] getMessage(String key, Class<?> clazz) {
    Properties prop = new Properties();
    try {/* w w  w. ja  v  a  2  s  .c  om*/
        prop.load(clazz.getResourceAsStream(clazz.getSimpleName() + ".properties"));
    } catch (IOException e) {
        // do nothing
    }
    return new String[] { prop.getProperty(key) };
}

From source file:org.sipfoundry.sipxconfig.test.XmlUnitHelper.java

/**
 * Loads XML document from class resource
 *
 * @param klass - for locating the file - pass this.class
 * @param name name of the file in the same directory as klass
 * @return newly read DOM4J document/* w w  w .  j av  a 2 s  . c  om*/
 */
public static Document loadDocument(Class klass, String name) throws DocumentException {
    InputStream stream = klass.getResourceAsStream(name);
    SAXReader reader = new SAXReader();
    reader.setValidation(false);
    return reader.read(stream);
}

From source file:Main.java

public static BufferedImage readIconImage(Class<?> clazz, String path) {
    Closer closer = Closer.create();/*from  w  ww .j  a va  2  s . c  om*/
    try {
        try {
            InputStream in = closer.register(clazz.getResourceAsStream(path));
            if (in != null) {
                return ImageIO.read(in);
            }
        } finally {
            closer.close();
        }
    } catch (IOException ignored) {
    }

    return null;
}