本文整理了Java中java.lang.Class.getResourceAsStream()
方法的一些代码示例,展示了Class.getResourceAsStream()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getResourceAsStream()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getResourceAsStream
[英]Returns a read-only stream for the contents of the given resource, or null if the resource is not found. The mapping between the resource name and the stream is managed by the class' class loader.
[中]返回给定资源内容的只读流,如果找不到资源,则返回null。资源名称和流之间的映射由类的类装入器管理。
代码示例来源:origin: bumptech/glide
/**
* 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);
}
}
代码示例来源:origin: square/okhttp
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.");
}
}
代码示例来源:origin: stackoverflow.com
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);
}
}
代码示例来源:origin: stackoverflow.com
// 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");
代码示例来源:origin: skylot/jadx
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);
}
代码示例来源:origin: skylot/jadx
@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;
}
}
代码示例来源:origin: skylot/jadx
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);
}
}
}
代码示例来源:origin: spring-projects/spring-framework
private InputStream createTestInputStream() {
return getClass().getResourceAsStream("testContentHandler.xml");
}
代码示例来源:origin: skylot/jadx
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);
}
}
代码示例来源:origin: spring-projects/spring-framework
public JAXBElement<Image> standardClassImage() throws IOException {
Image image = ImageIO.read(getClass().getResourceAsStream("spring-ws.png"));
return new JAXBElement<>(NAME, Image.class, image);
}
代码示例来源:origin: skylot/jadx
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;
}
代码示例来源:origin: spring-projects/spring-framework
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 + "]");
}
}
代码示例来源:origin: skylot/jadx
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();
}
代码示例来源:origin: spring-projects/spring-framework
@Test(expected = BeanDefinitionStoreException.class)
public void withInputSource() {
SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
InputSource resource = new InputSource(getClass().getResourceAsStream("test.xml"));
new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource);
}
代码示例来源:origin: spring-projects/spring-framework
@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);
}
}
代码示例来源:origin: spring-projects/spring-framework
@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);
}
代码示例来源:origin: spring-projects/spring-framework
@Test(expected = BeanDefinitionStoreException.class)
public void withOpenInputStream() {
SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml"));
new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource);
}
代码示例来源:origin: spring-projects/spring-framework
@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());
}
代码示例来源:origin: spring-projects/spring-framework
@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());
}
}
代码示例来源:origin: spring-projects/spring-framework
@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);
}
内容来源于网络,如有侵权,请联系作者删除!