Tabnine Logo
Class.getResourceAsStream
Code IndexAdd Tabnine to your IDE (free)

How to use
getResourceAsStream
method
in
java.lang.Class

Best Java code snippets using java.lang.Class.getResourceAsStream (Showing top 20 results out of 55,341)

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);
}
java.langClassgetResourceAsStream

Javadoc

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.

Popular methods of Class

  • getName
    Returns the name of the class represented by this Class. For a description of the format which is us
  • getSimpleName
  • getClassLoader
  • isAssignableFrom
    Determines if the class or interface represented by this Class object is either the same as, or is a
  • forName
    Returns the Class object associated with the class or interface with the given string name, using th
  • newInstance
    Returns a new instance of the class represented by this Class, created by invoking the default (that
  • getMethod
    Returns a Method object that reflects the specified public member method of the class or interface r
  • getSuperclass
    Returns the Class representing the superclass of the entity (class, interface, primitive type or voi
  • getConstructor
  • cast
    Casts an object to the class or interface represented by this Class object.
  • isInstance
  • getCanonicalName
    Returns the canonical name of the underlying class as defined by the Java Language Specification. Re
  • isInstance,
  • getCanonicalName,
  • getDeclaredField,
  • isArray,
  • getAnnotation,
  • getDeclaredFields,
  • getResource,
  • getDeclaredMethod,
  • getMethods

Popular in Java

  • Making http post requests using okhttp
  • scheduleAtFixedRate (Timer)
  • notifyDataSetChanged (ArrayAdapter)
  • getSharedPreferences (Context)
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • Socket (java.net)
    Provides a client-side TCP socket.
  • SecureRandom (java.security)
    This class generates cryptographically secure pseudo-random numbers. It is best to invoke SecureRand
  • Locale (java.util)
    Locale represents a language/country/variant combination. Locales are used to alter the presentatio
  • JFileChooser (javax.swing)
  • Loader (org.hibernate.loader)
    Abstract superclass of object loading (and querying) strategies. This class implements useful common
  • Top PhpStorm plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now