List of usage examples for java.lang Class getResourceAsStream
@CallerSensitive
public InputStream getResourceAsStream(String name)
From source file:com.roche.sequencing.bioinformatics.common.utils.Md5CheckSumUtil.java
public static String md5Sum(Class<?> resourceReferenceClass, String resourceName) throws FileNotFoundException, IOException { String md5Sum = null;//from w w w . j av a 2 s .c o m try (InputStream is = resourceReferenceClass.getResourceAsStream(resourceName)) { md5Sum = DigestUtils.md5Hex(is); } return md5Sum; }
From source file:org.apache.fop.events.DefaultEventBroadcaster.java
/** * Loads an event model and returns its instance. * @param resourceBaseClass base class to use for loading resources * @return the newly loaded event model. *//*from www .ja v a2 s .co m*/ private static EventModel loadModel(Class resourceBaseClass) { String resourceName = "event-model.xml"; InputStream in = resourceBaseClass.getResourceAsStream(resourceName); if (in == null) { throw new MissingResourceException("File " + resourceName + " not found", DefaultEventBroadcaster.class.getName(), ""); } try { return EventModelParser.parse(new StreamSource(in)); } catch (TransformerException e) { throw new MissingResourceException("Error reading " + resourceName + ": " + e.getMessage(), DefaultEventBroadcaster.class.getName(), ""); } finally { IOUtils.closeQuietly(in); } }
From source file:com.hp.ov.sdk.adaptors.StoragePoolSerializationAdapterTest.java
@BeforeClass public static void setupTest() throws IOException { Class<StoragePoolSerializationAdapterTest> clazz = StoragePoolSerializationAdapterTest.class; storagePoolJson = IOUtils.toString(clazz.getResourceAsStream("StoragePool.json"), "UTF-8"); storagePoolV2Json = IOUtils.toString(clazz.getResourceAsStream("StoragePoolV2.json"), "UTF-8"); }
From source file:org.apache.syncope.common.lib.PropertyUtils.java
public static Pair<Properties, String> read(final Class<?> clazz, final String propertiesFileName, final String confDirProp) { Properties props = new Properties(); String confDirName = null;//from w w w . j a va 2 s .c o m try (InputStream is = clazz.getResourceAsStream("/" + propertiesFileName)) { props.load(is); confDirName = props.getProperty(confDirProp); if (confDirName != null) { File confDir = new File(confDirName); if (confDir.exists() && confDir.canRead() && confDir.isDirectory()) { File confDirProps = new File(confDir, propertiesFileName); if (confDirProps.exists() && confDirProps.canRead() && confDirProps.isFile()) { props.clear(); props.load(new FileInputStream(confDirProps)); } } } } catch (Exception e) { throw new RuntimeException("Could not read " + propertiesFileName, e); } return Pair.of(props, confDirName); }
From source file:org.ow2.sirocco.cimi.server.resource.serialization.SerializationHelper.java
/** * Get a ressource in classpath and convert it as Reader. * //w w w. j a v a 2 s . co m * @param classLocation This class allows to find the package which is * located the ressoure * @param resourceName The name of ressource * @return The Reader for the ressource * @throws IOException In case of IO error */ public static Reader getResourceAsReader(final Class<?> classLocation, final String resourceName) throws IOException { InputStream in = classLocation.getResourceAsStream(resourceName); if (null == in) { throw new IOException("Resource not found : " + resourceName); } return new InputStreamReader(in); }
From source file:net.solarnetwork.node.util.ClassUtils.java
/** * Load a classpath SQL resource into a String. * //ww w. j a v a 2 s .c o m * @param resourceName * the SQL resource to load * @param clazz * the Class to load the resource from * @return the String */ public static String getResourceAsString(String resourceName, Class<?> clazz) { InputStream in = clazz.getResourceAsStream(resourceName); if (in == null) { throw new RuntimeException("Resource [" + resourceName + "] not found"); } ByteArrayOutputStream out = new ByteArrayOutputStream(); try { byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); return out.toString(); } catch (IOException e) { throw new RuntimeException("Error reading resource [" + resourceName + ']', e); } finally { try { in.close(); } catch (IOException ex) { LOG.warn("Could not close InputStream", ex); } try { out.close(); } catch (IOException ex) { LOG.warn("Could not close OutputStream", ex); } } }
From source file:io.wcm.testing.mock.osgi.OsgiMetadataUtil.java
/** * Try to read OSGI-metadata from /OSGI-INF and read all implemented interfaces and service properties * @param clazz OSGi service implementation class * @return Metadata document or null//from w w w . j a v a2 s . c om */ public static Document geDocument(Class clazz) { String metadataPath = "/OSGI-INF/" + clazz.getName() + ".xml"; InputStream metadataStream = clazz.getResourceAsStream(metadataPath); if (metadataStream == null) { return null; } try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); return documentBuilder.parse(metadataStream); } catch (ParserConfigurationException | SAXException | IOException ex) { throw new RuntimeException("Unable to read classpath resource: " + metadataPath, ex); } finally { try { metadataStream.close(); } catch (IOException ex) { // ignore } } }
From source file:Main.java
/** * get the file contents from resource as string * @param aFileName file name// ww w .j a va 2s. c om * @param aRetrieverClass retriever class * @return * @throws IOException */ public static String GetFileContentFromResourceAsString(String aFileName, Class aRetrieverClass) throws IOException { InputStream input = null; BufferedReader bReader = null; try { // read all the file content StringBuffer buffer = new StringBuffer(); input = aRetrieverClass.getResourceAsStream(aFileName); InputStreamReader isr = new InputStreamReader(input); bReader = new BufferedReader(isr); String line; while ((line = bReader.readLine()) != null) { buffer.append(line); buffer.append(System.getProperty("line.separator")); } return buffer.toString(); } finally { if (bReader != null) bReader.close(); if (input != null) input.close(); } }
From source file:Main.java
/** * Utility method that creates a <code>UIDefaults.LazyValue</code> that * creates an <code>ImageIcon</code> <code>UIResource</code> for the * specified image file name. The image is loaded using * <code>getResourceAsStream</code>, starting with a call to that method * on the base class parameter. If it cannot be found, searching will * continue through the base class' inheritance hierarchy, up to and * including <code>rootClass</code>. * * @param baseClass the first class to use in searching for the resource * @param rootClass an ancestor of <code>baseClass</code> to finish the * search at/*ww w. j a v a 2 s . co m*/ * @param imageFile the name of the file to be found * @return a lazy value that creates the <code>ImageIcon</code> * <code>UIResource</code> for the image, * or null if it cannot be found */ public static Object makeIcon(final Class<?> baseClass, final Class<?> rootClass, final String imageFile) { return new UIDefaults.LazyValue() { public Object createValue(UIDefaults table) { /* Copy resource into a byte array. This is * necessary because several browsers consider * Class.getResource a security risk because it * can be used to load additional classes. * Class.getResourceAsStream just returns raw * bytes, which we can convert to an image. */ byte[] buffer = java.security.AccessController .doPrivileged(new java.security.PrivilegedAction<byte[]>() { public byte[] run() { try { InputStream resource = null; Class<?> srchClass = baseClass; while (srchClass != null) { resource = srchClass.getResourceAsStream(imageFile); if (resource != null || srchClass == rootClass) { break; } srchClass = srchClass.getSuperclass(); } if (resource == null) { return null; } BufferedInputStream in = new BufferedInputStream(resource); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int n; while ((n = in.read(buffer)) > 0) { out.write(buffer, 0, n); } in.close(); out.flush(); return out.toByteArray(); } catch (IOException ioe) { System.err.println(ioe.toString()); } return null; } }); if (buffer == null) { return null; } if (buffer.length == 0) { System.err.println("warning: " + imageFile + " is zero-length"); return null; } return new ImageIconUIResource(buffer); } }; }
From source file:net.nicholaswilliams.java.teamcity.plugin.buildNumber.PluginFileUtils.java
public static void copyResource(Class<?> relativeClass, String resourceName, File destinationFile) { InputStream inputStream = null; OutputStream outputStream = null; try {/*from ww w .j av a2 s. c o m*/ inputStream = relativeClass.getResourceAsStream(resourceName); if (inputStream == null) { throw new RuntimeException("No resource with name [" + resourceName + "] found relative to class [" + relativeClass.getName() + "]."); } outputStream = new FileOutputStream(destinationFile); IOUtils.copy(inputStream, outputStream); } catch (IOException e) { throw new RuntimeException( "Failed to copy resource [" + resourceName + "] relative to class [" + relativeClass.getName() + "] to destination file [" + destinationFile.getAbsolutePath() + "]."); } finally { try { if (outputStream != null) outputStream.close(); } catch (IOException ignore) { } try { if (inputStream != null) inputStream.close(); } catch (IOException ignore) { } } }