/** * Returns an InputStream for the given test class and sub-path. * * @param testClass A Junit test class. * @param subPath The sub-path under androidTest/resources where the desired resource is * located. Should not be prefixed with a '/' */ public static InputStream openResource(Class<?> testClass, String subPath) { return testClass.getResourceAsStream("/" + subPath); } }
private static String versionString() { try { Properties prop = new Properties(); InputStream in = Main.class.getResourceAsStream("/okcurl-version.properties"); prop.load(in); in.close(); return prop.getProperty("version"); } catch (IOException e) { throw new AssertionError("Could not load okcurl-version.properties."); } }
package dummy; import java.io.*; public class Test { public static void main(String[] args) { InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt"); System.out.println(stream != null); stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt"); System.out.println(stream != null); } }
// From ClassLoader, all paths are "absolute" already - there's no context // from which they could be relative. Therefore you don't need a leading slash. InputStream in = this.getClass().getClassLoader() .getResourceAsStream("SomeTextFile.txt"); // From Class, the path is relative to the package of the class unless // you include a leading slash, so if you don't want to use the current // package, include a slash like this: InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");
public static TemplateFile fromResources(String path) throws FileNotFoundException { InputStream res = TemplateFile.class.getResourceAsStream(path); if (res == null) { throw new FileNotFoundException("Resource not found: " + path); } return new TemplateFile(path, res); }
@Nullable public static Font openFontTTF(String name) { String fontPath = "/fonts/" + name + ".ttf"; try (InputStream is = Utils.class.getResourceAsStream(fontPath)) { Font font = Font.createFont(Font.TRUETYPE_FONT, is); return font.deriveFont(12f); } catch (Exception e) { LOG.error("Failed load font by path: {}", fontPath, e); return null; } }
private void setEditorTheme(String editorThemePath) { try { editorTheme = Theme.load(getClass().getResourceAsStream(editorThemePath)); } catch (Exception e) { LOG.error("Can't load editor theme from classpath: {}", editorThemePath); try { editorTheme = Theme.load(new FileInputStream(editorThemePath)); } catch (Exception e2) { LOG.error("Can't load editor theme from file: {}", editorThemePath); } } }
private InputStream createTestInputStream() { return getClass().getResourceAsStream("testContentHandler.xml"); }
public void load() throws IOException, DecodeException { try (InputStream input = getClass().getResourceAsStream(CLST_FILENAME)) { if (input == null) { throw new JadxRuntimeException("Can't load classpath file: " + CLST_FILENAME); } load(input); } }
public JAXBElement<Image> standardClassImage() throws IOException { Image image = ImageIO.read(getClass().getResourceAsStream("spring-ws.png")); return new JAXBElement<>(NAME, Image.class, image); }
private Document loadXML(String xml) { Document doc; try (InputStream xmlStream = ManifestAttributes.class.getResourceAsStream(xml)) { if (xmlStream == null) { throw new JadxRuntimeException(xml + " not found in classpath"); } DocumentBuilder dBuilder = XmlSecurity.getSecureDbf().newDocumentBuilder(); doc = dBuilder.parse(xmlStream); } catch (Exception e) { throw new JadxRuntimeException("Xml load error, file: " + xml, e); } return doc; }
public CharacterEntityResourceIterator() { try { InputStream inputStream = getClass().getResourceAsStream(DTD_FILE); if (inputStream == null) { throw new IOException("Cannot find definition resource [" + DTD_FILE + "]"); } tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))); } catch (IOException ex) { throw new IllegalStateException("Failed to open definition resource [" + DTD_FILE + "]"); } }
private static void decodeAndroid() throws IOException { InputStream inputStream = new BufferedInputStream(ValuesParser.class.getResourceAsStream("/resources.arsc")); ResTableParser androidParser = new ResTableParser(); androidParser.decode(inputStream); androidStrings = androidParser.getStrings(); androidResMap = androidParser.getResStorage().getResourcesNames(); }
@Test(expected = BeanDefinitionStoreException.class) public void withInputSource() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml")); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); }
@Test // SPR-14882 public void shouldNotReadInputStreamResource() throws IOException { ResourceHttpMessageConverter noStreamConverter = new ResourceHttpMessageConverter(false); try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) { this.thrown.expect(HttpMessageNotReadableException.class); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); noStreamConverter.read(InputStreamResource.class, inputMessage); } }
@Test public void withInputSourceAndExplicitValidationMode() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml")); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD); reader.loadBeanDefinitions(resource); testBeanDefinitions(registry); }
@Test(expected = BeanDefinitionStoreException.class) public void withOpenInputStream() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml")); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); }
@Test public void shouldReadImageResource() throws IOException { byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg")); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); inputMessage.getHeaders().setContentDisposition( ContentDisposition.builder("attachment").filename("yourlogo.jpg").build()); Resource actualResource = converter.read(Resource.class, inputMessage); assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream()), is(body)); assertEquals("yourlogo.jpg", actualResource.getFilename()); }
@Test // SPR-13443 public void shouldReadInputStreamResource() throws IOException { try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) { MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); inputMessage.getHeaders().setContentDisposition( ContentDisposition.builder("attachment").filename("yourlogo.jpg").build()); Resource actualResource = converter.read(InputStreamResource.class, inputMessage); assertThat(actualResource, instanceOf(InputStreamResource.class)); assertThat(actualResource.getInputStream(), is(body)); assertEquals("yourlogo.jpg", actualResource.getFilename()); } }
@Test public void withOpenInputStreamAndExplicitValidationMode() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml")); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD); reader.loadBeanDefinitions(resource); testBeanDefinitions(registry); }