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.diorite.cfg.messages.MessageLoader.java

private static InputStreamReader getInputStreamReader(final String prefix, final Class<?> clazz,
        final String resourcesFolder, final Locale locale) {
    final InputStreamReader inputStreamReader = inputStreamToReader(
            clazz.getResourceAsStream(resourcesFolder + prefix + locale.toLanguageTag() + ".yml"));
    if (inputStreamReader == null) {
        return inputStreamToReader(
                clazz.getResourceAsStream(resourcesFolder + locale.toLanguageTag() + ".yml"));
    }//from   w  ww. j av a  2  s . co  m
    return inputStreamReader;
}

From source file:org.opendaylight.vtn.manager.it.option.TestOption.java

/**
 * Return an input stream associated with the given resource.
 *
 * @param cls   A {@link Class} instance used to load resource.
 * @param name  The name of the resource.
 * @return  An {@link InputStream} instance associated with the given
 *          resource.//from w  w  w  .  java 2 s .c  o m
 */
public static InputStream getResource(Class<?> cls, String name) {
    InputStream in = cls.getResourceAsStream("/" + name);
    if (in == null) {
        String msg = "Unable to load resource: " + name;
        throw new IllegalStateException(msg);
    }

    return in;
}

From source file:com.uber.jenkins.phabricator.utils.TestUtils.java

public static void addCopyBuildStep(FreeStyleProject p, final String fileName, final Class resourceClass,
        final String resourceName) {
    p.getBuildersList().add(new TestBuilder() {
        @Override/*  www.ja  v  a  2  s.co m*/
        public boolean perform(AbstractBuild build, Launcher launcher, BuildListener buildListener)
                throws InterruptedException, IOException {
            build.getWorkspace().child(fileName).copyFrom(resourceClass.getResourceAsStream(resourceName));
            return true;
        }
    });
}

From source file:com.siemens.sw360.datahandler.common.CommonUtils.java

public static Properties loadProperties(Class<?> clazz, String propertiesFilePath) {
    Properties props = new Properties();
    try {/* w  w w.  ja va 2  s .com*/
        try (InputStream resourceAsStream = clazz.getResourceAsStream(propertiesFilePath)) {
            if (resourceAsStream == null)
                throw new IOException("cannot open " + propertiesFilePath);

            props.load(resourceAsStream);
        }
    } catch (IOException e) {
        getLogger(clazz).error("Error opening resource " + propertiesFilePath + ", switching to default.", e);
    }
    return props;
}

From source file:org.silverpeas.file.FileUtil.java

/**
 * ---------------------------------------------------------------------
 * @param file/* www  . j a  va 2  s  .c  om*/
 * @param c
 * @return
 * @throws FileNotFoundException
 * @see
 */
public static InputStream getInputStream(File file, Class c) throws FileNotFoundException {
    InputStream rtn = null;
    if (file != null) {
        try {
            rtn = new FileInputStream(file);
        } catch (FileNotFoundException fnfex) {
            String s = file.toString();
            int i = s.indexOf(File.separator);
            if (i >= 0) {
                s = s.substring(i);
                s = StringUtil.sReplace("\\", "/", s);
                rtn = c.getResourceAsStream(s);
            }
            throw fnfex;
        }
    }
    return rtn;
}

From source file:eu.scape_project.tool.toolwrapper.core.utils.Utils.java

/**
 * Method useful to copy a particular resource (file under
 * src/main/resources) to a temporary directory
 * //from   www .j a v  a 2s .  c om
 * @param tempDir
 *            directory where the resource is being put on
 * @param resourcePath
 *            the path to the resource (beneath src/main/resources)
 * @param resourceName
 *            the name of the resource being copied
 * @param newName
 *            new name to be given to the resouce being copied
 * @param createTempDir
 *            flag on either the temporary directory needs to be created
 * @param invokingClass
 *            class to which the resource is related to
 * @return true if the resource was successfully copied, false otherwise
 * */
public static boolean copyResourceToTemporaryDirectory(File tempDir, String resourcePath, String resourceName,
        String newName, boolean createTempDir, Class<?> invokingClass) {
    boolean success = false;
    FileOutputStream fileOutputStream = null;
    try {
        if (createTempDir && !tempDir.mkdir()) {
            log.error("Error creating directory \"" + tempDir + "\"...");
        }
        File f = new File(tempDir, newName != null ? newName : resourceName);
        fileOutputStream = new FileOutputStream(f);
        IOUtils.copy(invokingClass.getResourceAsStream(resourcePath + resourceName), fileOutputStream);
        success = true;
    } catch (IOException e) {
        log.error(e);
    } finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }
    return success;
}

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

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

From source file:com.tesora.dve.standalone.PETest.java

public static void populateMetadata(Class<?> testClass, Properties props, String sqlRes) throws Exception {
    InputStream is = testClass.getResourceAsStream(sqlRes);
    if (is != null) {
        logger.info("Reading SQL statements from " + sqlRes);
        DBHelper dbh = new DBHelper(props).connect();
        try {// w ww. ja  va2s . c o  m
            dbh.executeFromStream(is);
        } finally {
            dbh.disconnect();
        }
        is.close();
    }
}

From source file:org.dkpro.tc.core.util.SaveModelUtils.java

private static String getCurrentTcVersionFromJar() {
    Class<?> contextClass = SaveModelUtils.class;

    InputStream resourceAsStream = contextClass
            .getResourceAsStream("/META-INF/maven/org.dkpro.tc/dkpro-tc-core/pom.xml");

    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;/*from w w w.  j  a  v  a2 s.  co  m*/
    try {
        model = reader.read(resourceAsStream);
    } catch (Exception e) {
        return null;
    }
    String version = model.getParent().getVersion();
    return version;
}

From source file:onl.area51.httpd.action.Actions.java

static void renderResource(Class<?> clazz, Request r, URI uri, String base) throws IOException {
    String url = uri.getPath();//w  ww  .  ja v  a  2 s. c o m
    if (url.contains("//")) {
        url = url.replace("//", "/");
    }
    if (!url.contains("/..")) {
        String path = base + (url.startsWith("/") ? "" : "/") + url;
        if (path.endsWith("/")) {
            path = path + "index.html";
        }
        InputStream is = clazz.getResourceAsStream(path);
        if (is != null) {
            if (r.isResponsePresent()) {
                // Hope the content type is the same, just add to the existing response.
                // This is usually due to including html content into an existing page
                try {
                    r.getResponse().copy(is);
                } finally {
                    is.close();
                }
            } else {
                // Treat as it's own entity
                r.getHttpResponse().setEntity(new InputStreamEntity(is));
            }
        }
    }
}