Example usage for java.lang ClassLoader getResourceAsStream

List of usage examples for java.lang ClassLoader getResourceAsStream

Introduction

In this page you can find the example usage for java.lang ClassLoader getResourceAsStream.

Prototype

public InputStream getResourceAsStream(String name) 

Source Link

Document

Returns an input stream for reading the specified resource.

Usage

From source file:com.shadwelldacunha.byteswipe.core.Utilities.java

public static void copyResource(String resource, String destination) throws IOException {
    ClassLoader classLoader = Utilities.class.getClassLoader();
    InputStream resStreamIn = classLoader.getResourceAsStream(resource);
    File resDestFile = new File(destination);
    OutputStream resStreamOut = new FileOutputStream(resDestFile);
    int readBytes;
    byte[] buffer = new byte[1024];
    while ((readBytes = resStreamIn.read(buffer)) > 0) {
        resStreamOut.write(buffer, 0, readBytes);
    }//from  ww w.  j a v a  2 s.  com
    resStreamIn.close();
    resStreamOut.close();
}

From source file:com.tc.util.factory.AbstractFactory.java

private static String findFactoryClassName(String id) {
    String serviceId = "META-INF/services/" + id;
    InputStream is = null;//from  w  w w.j ava 2  s  .co m

    try {
        ClassLoader cl = AbstractFactory.class.getClassLoader();
        if (cl != null) {
            is = cl.getResourceAsStream(serviceId);
        }

        if (is == null) {
            return System.getProperty(id);
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        String factoryClassName = null;
        try {
            factoryClassName = rd.readLine();
            rd.close();
        } catch (IOException x) {
            return System.getProperty(id);
        }

        if (factoryClassName != null && !"".equals(factoryClassName)) {
            return factoryClassName;
        }

        return System.getProperty(id);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.gtri.fhir.api.vistaex.rest.service.util.VistaUtil.java

/**
 * Finds a file on the classpath/*from w  w  w .j a v a  2s  .  co m*/
 * @param fileName the name of the file to find
 * @return the file.
 */
public static InputStream getFileInputStreamFromClassPath(String fileName) {
    //how to find resource in Servlet
    //http://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-configuration-resource-files-in-servlet-based-app/2161583#2161583
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(fileName);
    return inputStream;
}

From source file:net.sf.dynamicreports.report.defaults.Defaults.java

private static XmlDynamicReports load() {
    String resource = "dynamicreports-defaults.xml";
    InputStream is = null;/*from w  w  w . j  a  v a 2  s  .  c  om*/

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        is = classLoader.getResourceAsStream(resource);
    }
    if (is == null) {
        classLoader = Defaults.class.getClassLoader();
        if (classLoader != null) {
            is = classLoader.getResourceAsStream(resource);
        }
        if (is == null) {
            is = Defaults.class.getResourceAsStream("/" + resource);
        }
    }
    if (is == null) {
        return null;
    }

    try {
        Unmarshaller unmarshaller = JAXBContext.newInstance(XmlDynamicReports.class).createUnmarshaller();
        JAXBElement<XmlDynamicReports> root = unmarshaller.unmarshal(new StreamSource(is),
                XmlDynamicReports.class);
        return root.getValue();
    } catch (JAXBException e) {
        log.error("Could not load dynamic reports defaults", e);
        return null;
    }
}

From source file:com.github.restdriver.clientdriver.integration.SecureClientDriverTest.java

static KeyStore getKeystore()
        throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
    ClassLoader loader = SecureClientDriverTest.class.getClassLoader();
    byte[] binaryContent = IOUtils.toByteArray(loader.getResourceAsStream("keystore.jks"));
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(new ByteArrayInputStream(binaryContent), "password".toCharArray());
    return keyStore;

}

From source file:com.centeractive.ws.builder.utils.ResourceUtils.java

public static InputStream getResourceWithAbsolutePackagePathAsStream(Class<?> clazz, String absolutePackagePath,
        String resourceName) {/*from w w w.j av a2s. c o  m*/
    checkNotNull(clazz, "clazz cannot be null");
    String resourcePath = getResourcePath(absolutePackagePath, resourceName);
    InputStream resource = null;
    // first attempt - outside/inside jar file
    resource = clazz.getClass().getResourceAsStream(resourcePath);
    // second attempt - servlet container - inside application lib folder
    if (resource == null) {
        ClassLoader classLoader = clazz.getClass().getClassLoader();
        if (classLoader != null)
            resource = classLoader.getResourceAsStream(resourcePath);
    }
    return checkNotNull(resource, String.format("Resource [%s] loading failed", resourcePath));
}

From source file:com.hangum.tadpole.commons.sql.map.SQLMap.java

private static String getFileToString(String url) throws Exception {
    ClassLoader loader = SQLMap.class.getClassLoader();
    InputStream is = loader == null ? ClassLoader.getSystemResourceAsStream(url)
            : loader.getResourceAsStream(url);

    int size = is.available();
    byte[] dataByte = new byte[size];
    is.read(dataByte, 0, size);//from ww  w  . ja  v a2 s  .c  o m
    is.close();

    return new String(dataByte);
}

From source file:com.github.restdriver.clientdriver.integration.SecureClientDriverRuleTest.java

private static KeyStore getKeystore() {
    try {/*from   w ww. j  a v a2  s.com*/
        ClassLoader loader = SecureClientDriverTest.class.getClassLoader();
        byte[] binaryContent = IOUtils.toByteArray(loader.getResourceAsStream("keystore.jks"));
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(new ByteArrayInputStream(binaryContent), "password".toCharArray());
        return keyStore;
    } catch (Exception e) {
        throw new ClientDriverSetupException("Key store could not be loaded.", e);
    }
}

From source file:com.hangum.tadpole.engine.manager.internal.map.SQLMap.java

/**
 * SQLMap XML to string/*  w  w w  .j  a v  a 2  s .  c  om*/
 * 
 * @param url
 * @return
 * @throws Exception
 */
private static String getFileToString(String url) throws Exception {
    ClassLoader loader = SQLMap.class.getClassLoader();
    InputStream is = loader == null ? ClassLoader.getSystemResourceAsStream(url)
            : loader.getResourceAsStream(url);

    int size = is.available();
    byte[] dataByte = new byte[size];
    is.read(dataByte, 0, size);
    is.close();

    return new String(dataByte);
}

From source file:nl.denhaag.tw.comparators.gui.ReportWriter.java

private static void copyFileFromClasspath(String name, File outputDir) throws IOException {
    ClassLoader classLoader = (ClassLoader) Thread.currentThread().getContextClassLoader();
    InputStream image = classLoader.getResourceAsStream(name);
    copyFile(image, new File(outputDir, name));
    image.close();//from w w w .j ava  2  s.co m
}