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:at.bestsolution.persistence.java.Util.java

public static final String loadFile(Class<?> clazz, String name) {
    InputStream inputStream = clazz.getResourceAsStream(name);
    if (inputStream == null) {
        return null;
    }//from   ww w .  jav  a 2  s. com
    byte[] buf = new byte[1024];
    int l;
    StringBuilder b = new StringBuilder();
    try {
        while ((l = inputStream.read(buf)) != -1) {
            b.append(new String(buf, 0, l));
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b.toString();
}

From source file:com.attilax.zip.FileUtil.java

@SuppressWarnings("unchecked")
public static final Map<String, String> getPropertiesMap(Class clazz, String fileName) throws Exception {
    Properties properties = new Properties();
    InputStream inputStream = clazz.getResourceAsStream(fileName);
    if (inputStream == null)
        inputStream = clazz.getClassLoader().getResourceAsStream(fileName);
    properties.load(inputStream);/*w  w  w  . j  a v  a 2 s  .co m*/
    Map<String, String> map = new HashMap<String, String>();
    Set<Object> keySet = properties.keySet();
    for (Object key : keySet) {
        map.put((String) key, properties.getProperty((String) key));
    }
    return map;
}

From source file:com.clank.launcher.swing.SwingHelper.java

public static BufferedImage readIconImage(Class<?> clazz, String path) {
    InputStream in = null;/* w  w w . j  a v a2  s  .c  o  m*/
    try {
        in = clazz.getResourceAsStream(path);
        if (in != null) {
            return ImageIO.read(in);
        }
    } catch (IOException e) {
    } finally {
        closeQuietly(in);
    }
    return null;
}

From source file:gdt.data.grain.Support.java

/**
 * Get class resource as input stream. //  w w w  .jav a2 s. co m
 * @param handler the class
 * @param resource$ the resource name.
 * @return input stream.
 */
public static InputStream getClassResource(Class<?> handler, String resource$) {
    try {
        InputStream is = handler.getResourceAsStream(resource$);
        if (is != null) {
            //System.out.println("Support:getClassResource:resource stream="+is.toString());
            return is;
        } else {
            //   System.out.println("Support:getClassResource:cannot get embedded resource stream for handler="+handler.getName());                  
            ClassLoader classLoader = handler.getClassLoader();
            //if(classLoader!=null)
            //   System.out.println("Support:getClassResource:class loader="+classLoader.toString());
            is = classLoader.getResourceAsStream(resource$);
            //if(is!=null)
            //   System.out.println("Support:getClassResource:resourse stream="+is.toString());
            //else
            //   System.out.println("Support:getClassResource:cannot get resource stream");
            String handler$ = handler.getName();
            //System.out.println("Support:getClassResource:class="+handler$);
            String handlerName$ = handler.getSimpleName();
            //System.out.println("Support:getClassResource:class name="+handlerName$);
            String handlerPath$ = handler$.replace(".", "/");
            //System.out.println("Support:getClassResource:class path="+handlerPath$);
            String resourcePath$ = "src/" + handlerPath$.replace(handlerName$, resource$);
            //System.out.println("Support:getClassResource:resource path="+resourcePath$);
            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
            URL resourceUrl = classloader.getResource(resourcePath$);
            if (resourceUrl != null) {
                //System.out.println("Support:getClassResource:resource URL="+resourceUrl.toString());
                return resourceUrl.openStream();
            } else {
                //System.out.println("Support:getClassResource:cannot get resource URL");               
            }
        }
    } catch (Exception e) {
        Logger.getLogger(gdt.data.grain.Support.class.getName()).severe(e.toString());
    }
    return null;
}

From source file:org.opensaml.DefaultBootstrap.java

/**
 * Initializes the XMLTooling library with an explicitly supplied set of object providers.
 * /*from   w  ww .ja  va  2s .  c  o  m*/
 * @param providerConfigs list of provider configuration files located on the classpath
 * 
 * @throws ConfigurationException thrown if there is a problem loading the configuration files
 */
protected static void initializeXMLTooling(String[] providerConfigs) throws ConfigurationException {
    Logger log = getLogger();
    Class clazz = Configuration.class;
    XMLConfigurator configurator = new XMLConfigurator();

    for (String config : providerConfigs) {
        log.debug("Loading XMLTooling configuration {}", config);
        configurator.load(clazz.getResourceAsStream(config));
    }
}

From source file:com.t3.persistence.FileUtil.java

public static Object objFromResource(Class<?> loadingClass, String name) {
    try (InputStream is = loadingClass.getResourceAsStream(name)) {
        return Persister.newInstance().fromXML(is);
    } catch (IOException e) {
        throw new Error(e);
    }/*from   w  ww.j a v a  2 s .  co  m*/
}

From source file:com.sunchenbin.store.feilong.core.util.PropertiesUtil.java

/**
 * {@link Class#getResourceAsStream(String)}  InputStream,? {@link #getProperties(InputStream)}.
 * //from  ww  w  .j a v  a2 s .c o m
 * @param klass
 *            , klass 
 * @param propertiesPath
 *            ? class {@link PropertiesUtil},? src/main/resources?, messages/feilong-core-message_en_US.properties<br>
 *            <ul>
 *            <li> {@link #getProperties(Class, String)} ?
 *            {@code getProperties(PropertiesUtil.class,"/messages/feilong-core-message_en_US.properties")} <br>
 *            ?propertiesPath ?? <b>"/messages/feilong-core-message_en_US.properties"</b>, ???</li>
 *            <li> {@link #getPropertiesWithClassLoader(Class, String)} ?
 *            {@code getPropertiesWithClassLoader(PropertiesUtil.class,"messages/feilong-core-message_en_US.properties")}<br>
 *            ?propertiesPath ?? <b>"messages/feilong-core-message_en_US.properties"</b>,ClassLoader JVMBootstrapLoader?.<br>
 *            ??"messages/feilong-core-message_en_US.properties" ??/?)</li>
 *            </ul>
 * @return ?Properties
 * @see java.lang.Class#getResourceAsStream(String)
 * @see #getProperties(InputStream)
 * @see #getPropertiesWithClassLoader(Class, String)
 */
public static Properties getProperties(Class<?> klass, String propertiesPath) {
    // klass.getResourceAsStreamclassLoader.getResourceAsStream
    // ?,???ClassLoader??.
    InputStream inputStream = klass.getResourceAsStream(propertiesPath);
    return getProperties(inputStream);
}

From source file:com.feilong.commons.core.configure.PropertiesUtil.java

/**
 * {@link Class#getResourceAsStream(String)}  InputStream,? {@link #getProperties(InputStream)}.
 * //  w w w .j  a  v a2  s .  c  o  m
 * @param klass
 *            , klass 
 * @param propertiesPath
 *            ? class {@link PropertiesUtil},? src/main/resources?, messages/feilong-core-message_en_US.properties<br>
 *            <ul>
 *            <li> {@link #getProperties(Class, String)} ?
 *            {@code getProperties(PropertiesUtil.class,"/messages/feilong-core-message_en_US.properties")} <br>
 *            ?propertiesPath ?? <b>"/messages/feilong-core-message_en_US.properties"</b>, ???</li>
 *            <li> {@link #getPropertiesWithClassLoader(Class, String)} ?
 *            {@code getPropertiesWithClassLoader(PropertiesUtil.class,"messages/feilong-core-message_en_US.properties")}<br>
 *            ?propertiesPath ?? <b>"messages/feilong-core-message_en_US.properties"</b>,ClassLoader JVMBootstrapLoader?.<br>
 *            ??"messages/feilong-core-message_en_US.properties" ??/?)</li>
 *            </ul>
 * @return ?Properties
 * @see java.lang.Class#getResourceAsStream(String)
 * @see #getProperties(InputStream)
 * @see #getPropertiesWithClassLoader(Class, String)
 */
public static Properties getProperties(Class<?> klass, String propertiesPath) {
    // klass.getResourceAsStreamclassLoader.getResourceAsStream
    // ????ClassLoader??.
    InputStream inputStream = klass.getResourceAsStream(propertiesPath);
    return getProperties(inputStream);
}

From source file:org.roda_project.commons_ip.utils.Utils.java

public static Path copyResourceFromClasspathToDir(Class<?> resourceClass, Path dir, String resourceTempSuffix,
        String resourcePath) throws IOException, InterruptedException {
    try {//from  w w w.j  a  v  a  2 s. c om
        Path resource = Files.createTempFile(dir, "", resourceTempSuffix);
        InputStream inputStream = resourceClass.getResourceAsStream(resourcePath);
        OutputStream outputStream = Files.newOutputStream(resource);
        IOUtils.copy(inputStream, outputStream);
        inputStream.close();
        outputStream.close();
        return resource;
    } catch (ClosedByInterruptException e) {
        throw new InterruptedException();
    }
}

From source file:PropertiesUtil.java

public static Properties load(Properties properties, Class relativeClass, String relativeFileName)
        throws IOException {
    if (relativeClass == null) {
        throw new IllegalArgumentException("Relative Class is not set.");
    }// w w w . j av  a2  s.co  m
    if (relativeFileName == null) {
        throw new IllegalArgumentException("Relative File Name is not set.");
    } else {
        return load(properties, relativeClass.getResourceAsStream(relativeFileName));
    }
}