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:LabelJarSample.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;/*from   w  w  w  .j a  va2s. co  m*/
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            returnValue = Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:com.liferay.portal.search.elasticsearch.internal.util.ResourceUtil.java

public static String getResourceAsString(Class<?> clazz, String resourceName) {

    try (InputStream inputStream = clazz.getResourceAsStream(resourceName)) {

        return StringUtil.read(inputStream);
    } catch (IOException ioe) {
        throw new RuntimeException("Unable to load resource: " + resourceName, ioe);
    }/*from   w  w w  .j  av  a  2s .c o  m*/
}

From source file:Main.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;//w w  w .  j av a  2 s .  c om
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            Toolkit t = Toolkit.getDefaultToolkit();

            returnValue = t.createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:org.eclipse.swt.examples.graphics.AdvancedGraphics.java

static Image loadImage(Device device, Class<AdvancedGraphics> clazz, String string) {
    InputStream stream = clazz.getResourceAsStream(string);
    if (stream == null)
        return null;
    Image image = null;//w  ww . j  a va 2 s .c o m
    try {
        image = new Image(device, stream);
    } catch (SWTException ex) {
    } finally {
        try {
            stream.close();
        } catch (IOException ex) {
        }
    }
    return image;
}

From source file:org.openmrs.projectbuendia.webservices.rest.XmlTestUtil.java

static String readResourceAsString(Class<?> clazz, String file) throws IOException {
    return IOUtils.toString(clazz.getResourceAsStream(file), "utf-8");
}

From source file:org.sakuli.utils.ResourceHelper.java

public static String getClasspathResourceAsStream(Class<?> classDef, String classPathResource)
        throws IOException {
    InputStream in = classDef.getResourceAsStream(classPathResource);
    return IOUtils.toString(in, StandardCharsets.UTF_8);
}

From source file:org.apache.solr.kelvin.testcases.SimpleConditionTest.java

public static JsonNode readJsonResource(Class<?> c, String resourceName) throws IOException {
    String raw = IOUtils.toString(c.getResourceAsStream(resourceName), "utf8");
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(raw, JsonNode.class);
}

From source file:org.openo.sdnhub.osdriverservice.samples.Utils.java

public static String getSampleJson(@SuppressWarnings("rawtypes") Class clz, String fileName)
        throws IOException {
    String json = IOUtils.toString(clz.getResourceAsStream(fileName));
    return json;//from   w w w  .ja v a 2 s.  c o m
}

From source file:com.groupon.jenkins.util.ResourceUtils.java

private static String readFile(Class<?> resourceClass, String ymlResource) {
    InputStream base = null;//w ww. j  av a2  s.c o m
    try {
        base = resourceClass.getResourceAsStream(ymlResource);
        return IOUtils.toString(base);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(base);
    }
}

From source file:objective.taskboard.utils.IOUtilities.java

public static String resourceToString(Class<?> clazz, String path) {
    InputStream inputStream = clazz.getResourceAsStream(path);
    if (inputStream == null)
        return null;
    try {/*from  ww  w. ja v a2 s. com*/
        return IOUtils.toString(inputStream, ENCODE_UTF_8);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}