Example usage for org.springframework.core.io Resource getInputStream

List of usage examples for org.springframework.core.io Resource getInputStream

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getInputStream.

Prototype

InputStream getInputStream() throws IOException;

Source Link

Document

Return an InputStream for the content of an underlying resource.

Usage

From source file:com.googlecode.flyway.core.util.ResourceUtils.java

/**
 * Loads this resource in a string using this encoding.
 *
 * @param resource The resource to load.
 * @param encoding The encoding of the resource.
 * @return The resource contents as a string.
 *//*from www  .ja  v a  2 s . c o  m*/
public static String loadResourceAsString(Resource resource, String encoding) {
    try {
        Reader reader = new InputStreamReader(resource.getInputStream(), Charset.forName(encoding));
        return FileCopyUtils.copyToString(reader);
    } catch (IOException e) {
        throw new IllegalStateException(
                "Unable to load resource: " + resource.getDescription() + " (encoding: " + encoding + ")", e);
    }
}

From source file:org.sakaiproject.genericdao.springutil.ResourceFinder.java

/**
 * Resolves a list of paths into resources within the current classloader
 * @param paths a list of paths to resources (org/sakaiproject/mystuff/Thing.xml)
 * @return an array of InputStreams/*  w w w.ja  v a  2  s.co m*/
 */
public static InputStream[] getInputStreams(List<String> paths) {
    List<Resource> rs = makeResources(paths);
    InputStream[] streams = new InputStream[rs.size()];
    for (int i = 0; i < rs.size(); i++) {
        Resource r = rs.get(i);
        try {
            streams[i] = r.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException("Failed to get inputstream for: " + r.getFilename(), e);
        }
    }
    return streams;
}

From source file:org.adsync4j.testutils.ldap.EmbeddedLdapServerFactoryBean.java

private static InputStream resourceToInputStream(Resource resource) {
    try {/*  ww w .ja v a  2 s .  c  o m*/
        return resource.getInputStream();
    } catch (IOException e) {
        throw propagate(e);
    }
}

From source file:Main.java

/**
 * @param resource//w ww  .j  a v  a 2  s.  c  o m
 *        the XML resource
 * @return {@link Document}
 * @throws IOException
 *         on exception
 * @throws ParserConfigurationException
 *         on exception
 * @throws SAXException
 *         on exception
 */
public static Document readDocument(final Resource resource)
        throws IOException, ParserConfigurationException, SAXException {
    final InputStream is = resource.getInputStream();
    if (is != null) {
        try {
            final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            final DocumentBuilder db = dbf.newDocumentBuilder();
            return db.parse(is);
        } finally {
            is.close();
        }
    }
    return null;

}

From source file:com.cloudseal.spring.client.namespace.CloudSealKeyManagerImpl.java

protected static KeyStore createKeyStore(Resource storeFile, String storePass, String storeType) {
    InputStream inputStream = null;
    try {/*from www. j  a  v a2 s . c o  m*/
        inputStream = storeFile.getInputStream();
        KeyStore ks = KeyStore.getInstance(storeType);
        ks.load(inputStream, storePass.toCharArray());
        return ks;
    } catch (Exception e) {
        throw new RuntimeException("Error initializing keystore", e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.carewebframework.ui.test.CommonTest.java

/**
 * Reads text from the specified resource on the classpath.
 * //from  w  ww  .  ja v  a  2s  .c  om
 * @param resourceName Name of the resource.
 * @return Text read from the resource.
 * @throws IOException IO exception.
 */
public static String getTextFromResource(String resourceName) throws IOException {
    Resource resource = desktopContext.getResource("classpath:" + resourceName);
    InputStream is = resource.getInputStream();
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];

    try {
        Reader reader = new BufferedReader(new InputStreamReader(is, StrUtil.CHARSET));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
    return writer.toString();
}

From source file:com.formkiq.core.util.Resources.java

/**
 * Gets a file from classpath as bytes./*from   www.  j  av  a  2  s  .c  om*/
 * @param file {@link String}
 * @return {@link String}
 * @throws IOException IOException
 */
public static InputStream getResourceAsInputStream(final String file) throws IOException {

    InputStream is = null;
    Resource resource = new ClassPathResource(file);

    try {
        is = resource.getInputStream();
    } catch (FileNotFoundException e) {

        // check for file in tomcat conf directory
        String catalinabase = System.getProperty("catalina.base");

        if (StringUtils.hasText(catalinabase)) {
            File configDir = new File(catalinabase, "conf");
            File configFile = new File(configDir, file);

            if (configFile.exists()) {
                is = new FileInputStream(configFile);
            }
        }
    }

    return is;
}

From source file:ch.algotrader.config.spring.ConfigLoader.java

static void loadResource(final Map<String, String> paramMap, final Resource resource) throws IOException {

    try (InputStream inputStream = resource.getInputStream()) {
        Properties props = new Properties();
        props.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));

        for (Map.Entry<Object, Object> entry : props.entrySet()) {
            String paramName = (String) entry.getKey();
            String paramValue = (String) entry.getValue();
            if (StringUtils.isNotBlank(paramName)) {
                paramMap.put(paramName, paramValue);
            }// w  ww  .j  av  a2 s.  c  om
        }

    }
}

From source file:com.doculibre.constellio.utils.SolrSchemaUtils.java

public static IndexSchema getSchema(ConnectorType connectorType) {
    IndexSchema connectorTypeSchema;/*from  w  w  w  .  j  ava2  s.c  o  m*/

    String targetSchemaName;
    if (connectorType != null) {
        targetSchemaName = connectorType.getName();
    } else {
        targetSchemaName = "constellio"; // Default
    }

    Resource[] classpathResources = ConstellioSpringUtils.getResources(SOLR_SCHEMA_PATTERN);
    if (classpathResources != null) {
        connectorTypeSchema = null;
        for (int i = 0; i < classpathResources.length; i++) {
            InputStream resourceInput = null;
            try {
                Resource resource = classpathResources[i];
                resourceInput = resource.getInputStream();
                Document schema = new SAXReader().read(resourceInput);
                Element schemaElement = schema.getRootElement();
                String schemaName = schemaElement.attributeValue("name");
                if (targetSchemaName.equals(schemaName)) {
                    resourceInput = resource.getInputStream();

                    SolrConfig dummySolrConfig;
                    URL dummySolrConfigURL = SolrSchemaUtils.class.getClassLoader()
                            .getResource("config" + File.separator + "solrdefault" + File.separator);
                    File dummySolrConfigDir = new File(dummySolrConfigURL.toURI());
                    File dummySolrConfigFile = new File(dummySolrConfigDir,
                            "conf" + File.separator + "solrconfig.xml");
                    dummySolrConfig = new SolrConfig(dummySolrConfigDir.getPath(),
                            dummySolrConfigFile.getPath(), null);

                    connectorTypeSchema = new IndexSchema(dummySolrConfig, null,
                            new InputSource(resourceInput));
                    break;
                }
            } catch (DocumentException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ParserConfigurationException e) {
                throw new RuntimeException(e);
            } catch (SAXException e) {
                throw new RuntimeException(e);
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            } catch (Exception e) {
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else {
                    throw new RuntimeException(e);
                }
            } finally {
                IOUtils.closeQuietly(resourceInput);
            }
        }
    } else {
        connectorTypeSchema = null;
    }
    return connectorTypeSchema;
}

From source file:com.flipkart.phantom.runtime.impl.spring.utils.ConfigFileUtils.java

/**
 * Gets the contents of a <code>Resource</code> in a single String
 * @param resource Resource to be read//w  w  w .j a  va 2  s  .  co m
 * @return Contents as a <code>String<code/>
 */
public static String getContents(Resource resource) {
    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(resource.getInputStream(), writer, "UTF-8");
    } catch (IOException e) {
        LOGGER.error("Exception while reading file " + resource.getFilename(), e);
    }
    return writer.toString();
}