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:org.ebayopensource.twin.Attachment.java

/**
 * Replace the contents of the remote file with data from the given resource (evaluated relative to the given class).
 * @see Application#upload(String, Class)
 *//*from   w  w  w  .j  a  v a  2s .  c  o m*/
public void setContents(String resource, Class<?> context) throws IOException, TwinException {
    if (context == null) {
        context = Attachment.class;
        if (!resource.startsWith("/"))
            resource = "/" + resource;
    }
    setContents(context.getResourceAsStream(resource));
}

From source file:li.strolch.runtime.main.MainStarter.java

/**
 * @param appClass//from  www  . jav a2s  .  com
 *            See {@link StrolchBootstrapper#StrolchBootstrapper(Class)}
 */
public MainStarter(Class<?> appClass) {
    DBC.PRE.assertNotNull("appClass must be set!", appClass);

    Properties env = new Properties();
    try (InputStream in = appClass.getResourceAsStream(StrolchBootstrapper.APP_VERSION_PROPERTIES)) {
        env.load(in);
    } catch (Exception e) {
        throw new IllegalArgumentException("Could not find resource "
                + StrolchBootstrapper.APP_VERSION_PROPERTIES + " on ClassLoader of class " + appClass);
    }

    StrolchVersion appVersion = new StrolchVersion(env);
    this.appVersion = appVersion;

    configureOptions();
}

From source file:name.martingeisse.reporting.renderer.HtmlRenderer.java

/**
 * @param c//from   ww  w.  jav  a 2  s  . c  o  m
 * @param name
 */
private void printFromClasspathResource(Class<?> c, String name) {
    try {
        InputStream s = c.getResourceAsStream(name);
        IOUtils.copy(s, out, "utf-8");
        s.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.falcon.util.ApplicationProperties.java

private InputStream checkClassPath(String propertyFileName) {

    InputStream resourceAsStream = null;
    Class clazz = ApplicationProperties.class;
    URL resource = clazz.getResource("/" + propertyFileName);
    if (resource != null) {
        LOG.info("Fallback to classpath for: {}", resource);
        resourceAsStream = clazz.getResourceAsStream("/" + propertyFileName);
    } else {/* www .ja v a2s . c  om*/
        resource = clazz.getResource(propertyFileName);
        if (resource != null) {
            LOG.info("Fallback to classpath for: {}", resource);
            resourceAsStream = clazz.getResourceAsStream(propertyFileName);
        }
    }
    return resourceAsStream;
}

From source file:com.qmetry.qaf.automation.util.PropertyUtil.java

/**
 * load property inside java/jar package
 * /*from  w  w w  . j  a v  a  2 s .c  o m*/
 * @param cls
 * @param propertyFile
 * @return
 */
public boolean load(Class<?> cls, String propertyFile) {
    boolean success = false;
    InputStream in = null;
    try {
        in = cls.getResourceAsStream(propertyFile);
        load(in);
        success = true;
    } catch (Exception e) {
        logger.error("Unable to load properties from file:" + propertyFile, e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
    return success;
}

From source file:name.martingeisse.api.handler.misc.ClasspathResourceHandler.java

@Override
public void handle(final ApiRequestCycle requestCycle, final ApiRequestPathChain path) throws Exception {
    HttpServletResponse response = requestCycle.getResponse();

    // set headers
    if (characterEncoding != null) {
        response.setCharacterEncoding(characterEncoding);
    }// w  w w  .j  a va  2 s  . c o  m
    response.setContentType(contentType);

    // write the resource to the response body
    Class<?> scope = (this.classpathScope == null ? getClass() : this.classpathScope);
    String packageName = scope.getPackage().getName();
    String internalPath = "/" + packageName.replace('.', '/') + '/' + classpathFilename;
    try (InputStream inputStream = scope.getResourceAsStream(internalPath)) {
        if (inputStream == null) {
            throw new Exception("classpath resource not found: " + internalPath);
        }
        try (OutputStream outputStream = requestCycle.getOutputStream()) {
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
        }
    }

}

From source file:com.seleniumtests.reporter.SeleniumTestsReporter.java

public static void writeResourceToFile(final File file, final String resourceName, final Class<?> aClass)
        throws IOException {
    final InputStream inputStream = aClass.getResourceAsStream("/" + resourceName);
    if (inputStream == null) {
        logger.error("can not find resource on the class path: " + resourceName);
    } else {/*ww  w. ja  v  a  2  s .  co  m*/

        try {
            final FileOutputStream outputStream = new FileOutputStream(file);
            try {
                int i;
                final byte[] buffer = new byte[4096];
                while (0 < (i = inputStream.read(buffer))) {
                    outputStream.write(buffer, 0, i);
                }
            } finally {
                outputStream.close();
            }
        } finally {
            inputStream.close();
        }
    }
}

From source file:org.rhq.helpers.bundleGen.XmlQuestionsReader.java

/**
 * Present the questions to the user and read answers from the provided <i>reader</i>.
 * @param reader Buffered Reader to read from. Usually an InputStreamReader(System.in)
 * @param props Props object to fill the answers into.
 * @throws FileNotFoundException if no question file is found
 * @throws Exception On various other occasions
 *//*from   ww  w .j  av a 2 s . co  m*/
public void readQuestions(BufferedReader reader, Props props) throws Exception {

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Class<? extends XmlQuestionsReader> clazz = getClass();
        String lang = Locale.getDefault().getLanguage();
        InputStream input = clazz.getResourceAsStream("/" + baseName + ".xml");
        if (input == null) {
            throw new FileNotFoundException("Input file [" + baseName + "] not found");
        }

        Document doc = builder.parse(input);
        Element root = doc.getDocumentElement(); // <questions>
        NodeList questionNodes = root.getChildNodes();//  "question|block"

        for (int i = 0; i < questionNodes.getLength(); i++) {
            Node node = questionNodes.item(i);
            if (node instanceof Element) {
                String nodeName = node.getNodeName();
                if (nodeName.equals("question")) {
                    Element question = (Element) node;
                    String res = handleQuestion(reader, question, lang);
                    String prop = question.getAttribute("prop");
                    String type = question.getAttribute("type");
                    fillObject(props, prop, type, res);
                } else if (nodeName.equals("block")) {
                    //                  Map res = handleBlock(reader,node,props,lang);
                    // TODO
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cz.lbenda.rcp.IconFactory.java

public <T> Image image(T caller, String base, IconSize iconSize) {
    final Class clazz;
    if (caller == null) {
        clazz = this.getClass();
    } else if (caller instanceof Class) {
        clazz = (Class) caller;/* ww  w  .  j  av  a2 s  .c o  m*/
    } else {
        clazz = caller.getClass();
    }
    String iconName = iconName(base, iconSize);
    InputStream is = clazz.getResourceAsStream(iconName);
    if (is == null) {
        String iconName2 = iconName("unknown.png", iconSize);
        LOG.warn("Icon with name not exist '" + iconName + "' '" + iconName2 + "' used instead of.");
        is = IconFactory.class.getResourceAsStream(iconName2);
    }
    return new Image(is);
}