List of usage examples for java.lang Class getResourceAsStream
@CallerSensitive
public InputStream getResourceAsStream(String name)
From source file:ca.oson.json.util.ObjectUtil.java
public static String[] getParameterNames(Method m) throws IOException { Class<?> declaringClass = m.getDeclaringClass(); String resourceName = "/" + declaringClass.getName().replace('.', '/') + ".class"; InputStream classData = declaringClass.getResourceAsStream(resourceName); VariableReader variableDiscoverer = new VariableReader(); ClassReader r = new ClassReader(classData); r.accept(variableDiscoverer, 0);//w w w . j a v a2 s . co m Map<Integer, String> variableNames = variableDiscoverer.getVariableNames(m); String[] parameterNames = new String[m.getParameterTypes().length]; if (variableNames != null) { for (int i = 0; i < parameterNames.length; i++) { parameterNames[i] = variableNames.get(i); } } return parameterNames; }
From source file:com.baeldung.file.FileOperationsTest.java
@Test public void givenFileName_whenUsingJarFile_thenFileData() throws IOException { String expectedData = "BSD License"; Class clazz = Matchers.class; InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt"); String data = readFromInputStream(inputStream); Assert.assertThat(data.trim(), CoreMatchers.containsString(expectedData)); }
From source file:ch.entwine.weblounge.common.impl.util.TemplateUtils.java
/** * Loads the resource identified by concatenating the package name from * <code>clazz</code> and <code>path</code> from the classpath. * //from w ww .ja v a2 s. co m * @param path * the path relative to the package name of <code>clazz</code> * @param clazz * the class * @param language * the requested language * @param site * the associated site * @return the resource */ public static String load(String path, Class<?> clazz, Language language, Site site) { if (path == null) throw new IllegalArgumentException("path cannot be null"); if (clazz == null) throw new IllegalArgumentException("clazz cannot be null"); String pkg = null; if (!path.startsWith("/")) pkg = "/" + clazz.getPackage().getName().replace('.', '/'); // Try to find the template in any of the usual languages InputStream is = null; String[] templates = null; if (site != null) templates = LanguageUtils.getLanguageVariants(path, language, site.getDefaultLanguage()); else templates = LanguageUtils.getLanguageVariants(path, language); for (String template : templates) { String pathToTemplate = pkg != null ? UrlUtils.concat(pkg, template) : template; is = clazz.getResourceAsStream(pathToTemplate); if (is != null) { path = template; break; } } // If is is still null, then the template doesn't exist. if (is == null) { logger.error("Template " + path + " not found in any language"); return null; } // Load the template InputStreamReader isr = null; StringBuffer buf = new StringBuffer(); try { logger.debug("Loading " + path); isr = new InputStreamReader(is, Charset.forName("UTF-8")); char[] chars = new char[1024]; int count = 0; while ((count = isr.read(chars)) > 0) { for (int i = 0; i < count; i++) buf.append(chars[i]); } return buf.toString(); } catch (Throwable t) { logger.warn("Error reading " + path + ": " + t.getMessage()); } finally { IOUtils.closeQuietly(isr); IOUtils.closeQuietly(is); } logger.debug("Template " + path + " loaded"); return null; }
From source file:com.baeldung.file.FileOperationsManualTest.java
@Test public void givenFileName_whenUsingJarFile_thenFileData() throws IOException { String expectedData = "BSD License"; Class clazz = Matchers.class; InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt"); String data = readFromInputStream(inputStream); assertThat(data.trim(), CoreMatchers.containsString(expectedData)); }
From source file:com.baeldung.file.FileOperationsUnitTest.java
@Test public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException { String expectedData = "Hello World from fileTest.txt!!!"; Class clazz = FileOperationsUnitTest.class; InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt"); String data = readFromInputStream(inputStream); Assert.assertEquals(expectedData, data.trim()); }
From source file:com.baeldung.file.FileOperationsTest.java
@Test public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException { String expectedData = "Hello World from fileTest.txt!!!"; Class clazz = FileOperationsTest.class; InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt"); String data = readFromInputStream(inputStream); Assert.assertEquals(expectedData, data.trim()); }
From source file:com.baeldung.file.FileOperationsManualTest.java
@Test public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException { String expectedData = "Hello World from fileTest.txt!!!"; Class clazz = FileOperationsManualTest.class; InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt"); String data = readFromInputStream(inputStream); assertEquals(expectedData, data.trim()); }
From source file:com.googlecode.wickedcharts.showcase.StringFromResourceModel.java
public StringFromResourceModel(Class<?> scope, String resourceName) { InputStream in = null;/* www. ja v a2 s . c om*/ BufferedReader reader = null; try { in = scope.getResourceAsStream(resourceName); reader = new BufferedReader(new InputStreamReader(in)); StringBuffer stringBuffer = new StringBuffer(); String line; while ((line = reader.readLine()) != null) { stringBuffer.append(line); stringBuffer.append("\n"); } this.modelObject = stringBuffer.toString(); } catch (IOException e) { throw new RuntimeException( "Error reading resource '" + resourceName + "' from class '" + scope.getName() + "'.", e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(reader); } }
From source file:com.microsoft.tfs.core.internal.db.Configuration.java
public Configuration(final Class cls, final String resourceName) { InputStream input = null;// w w w. j a v a 2s.co m try { input = cls.getResourceAsStream(resourceName); if (input != null) { props.load(input); } else { log.warn(MessageFormat.format("configuration [{0}] from class [{1}] does not exist", //$NON-NLS-1$ resourceName, cls.getName())); } } catch (final IOException ex) { log.warn( MessageFormat.format("error loading configuration [{0}] from class [{1}]", resourceName, //$NON-NLS-1$ cls.getName()), ex); } finally { if (input != null) { try { input.close(); } catch (final IOException e) { log.warn(MessageFormat.format("error closing configuration [{0}] from class [{1}]", //$NON-NLS-1$ resourceName, cls.getName()), e); } } } }
From source file:org.eclipse.emf.teneo.annotations.mapper.PersistenceFileProvider.java
/** * Returns an InputStream with the file content, note if the file does not exist then null may * be returned. This implementation searches for the file in the classpath using the path * parameters.//from ww w.j a va2s .c om * * Custom implementations of this class may use any other method to find the file. * * @param clz * the class to use when reading the file through a classloader * @param path * the path to the file (incl. the filename and extension) * * @return an InputStream if found, or null otherwise */ public InputStream getFileContent(Class<?> clz, String path) { if (clz == null) { return this.getClass().getClassLoader().getResourceAsStream(path); } else { return clz.getResourceAsStream(path); } }