Java Class.getResourceAsStream Examples

Java Class.getResourceAsStream - 30 examples found. These are the top rated real world Java examples of Class.getResourceAsStream extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  public void addExtension(Class<?> loadResourcesUsing, String loadFrom) throws IOException {
    // Is loadFrom a file?
    File file = new File(loadFrom);
    if (file.exists()) {
      addExtension(file);
      return;
    }

    // Try and load it from the classpath
    InputStream resource = loadResourcesUsing.getResourceAsStream(loadFrom);
    if (resource == null && !loadFrom.startsWith("/")) {
      resource = loadResourcesUsing.getResourceAsStream("/" + loadFrom);
    }
    if (resource == null) {
      resource = FirefoxProfile.class.getResourceAsStream(loadFrom);
    }
    if (resource == null && !loadFrom.startsWith("/")) {
      resource = FirefoxProfile.class.getResourceAsStream("/" + loadFrom);
    }
    if (resource == null) {
      throw new FileNotFoundException("Cannot locate resource with name: " + loadFrom);
    }

    File root;
    if (FileHandler.isZipped(loadFrom)) {
      root = FileHandler.unzip(resource);
    } else {
      throw new RuntimeException("Will only install zipped extensions for now");
    }

    addExtension(root);
  }
  /** Tests loading a resource with a global name. */
  @TestTargetNew(
      level = TestLevel.PARTIAL_COMPLETE,
      notes = "",
      method = "getResourceAsStream",
      args = {java.lang.String.class})
  public void testGetResourceAsStream2() throws IOException {
    Class clazz = getClass();

    InputStream stream =
        clazz.getResourceAsStream("/org/apache/harmony/luni/tests/java/lang/HelloWorld.txt");
    assert (stream != null);

    byte[] buffer = new byte[20];
    int length = stream.read(buffer);
    String s = new String(buffer, 0, length);
    assert ("Hello, World.".equals(s));

    stream.close();

    try {
      clazz.getResourceAsStream(null);
      fail("NullPointerException is not thrown.");
    } catch (NullPointerException npe) {
      // expected
    }
    assertNull(clazz.getResourceAsStream("/NonExistentResource"));
    assertNull(clazz.getResourceAsStream("org/apache/harmony/luni/tests/java/lang/HelloWorld.txt"));
  }
  /*--------------------------------------------------------------------------*/
  private InputStream openInputStream(String name, NativeLibraryClient client) throws Exception {
    Class clientClass = client.getClass();
    // ----------------------------------------------------
    // try to open an input stream, assuming the library
    // is located with the client
    // ----------------------------------------------------
    InputStream input = clientClass.getResourceAsStream(name + extension);

    // ----------------------------------------------------
    // if this is not successful, try to load from the
    // location where all native libraries are supposed
    // to be located.
    // ----------------------------------------------------
    if (input == null) {
      input = clientClass.getResourceAsStream('/' + nativeDirectory + '/' + name + extension);
    }

    // ----------------------------------------------------
    // if this fails as well, throw an exception
    // ----------------------------------------------------
    if (input == null) {
      throw (new Exception("can't locate library"));
    } else {
      return (input);
    }
  }
Example #4
0
 public static InputStream getSeleniumResourceAsStream(String resourceFile) {
   Class clazz = LauncherUtils.class;
   InputStream input = clazz.getResourceAsStream(resourceFile);
   if (input == null) {
     try {
       // This is hack for the OneJar version of Selenium-Server.
       // Examine the contents of the jar made by
       // https://svn.openqa.org/svn/selenium-rc/trunk/selenium-server-onejar/build.xml
       clazz = Class.forName("OneJar");
       input = clazz.getResourceAsStream(resourceFile);
     } catch (ClassNotFoundException e) {
     }
   }
   return input;
 }
  /**
   * Get an InputStream for a particular file name - first check the sakai.home area and then revert
   * to the classpath.
   *
   * <p>This is a utility method used several places.
   */
  public static java.io.InputStream getConfigStream(String fileName, Class curClass) {
    // Within Sakai default path is usually tomcat/sakai/file.properties
    // Sakai deployers can move this.

    // When we area not in Sakai's JVM, this may be several places
    // depending on the JVM/OS, etc
    //  - the directory where we started Tomcat
    //  - the user's hojme directory
    //  - the root directory of the system
    // Also the user can start the portal JVN with -Dsakai.home= to force this path

    String sakaiHome = System.getProperty("sakai.home");
    String filePath = sakaiHome + fileName;
    // System.out.println("filePath="+filePath);

    try {
      java.io.File f = new java.io.File(filePath);
      if (f.exists()) {
        return new java.io.FileInputStream(f);
      }
    } catch (Throwable t) {
      // Not found in the sakai.home area
    }

    // See if we can find this property file relative to a  class loader
    if (curClass == null) return null;

    java.io.InputStream istream = null;

    // TODO: Figure out *where* the file really needs to go to
    // trigger this first section of code. It would be cool
    // to have this be shared/lib or somewhere - I just cannot
    // figure this out at this point - Chuck

    // Load from the class loader
    istream = curClass.getClassLoader().getResourceAsStream(fileName);
    if (istream != null) return istream;

    // Load from the webapp class relative
    // tomcat/webapps/sakai-webapp/WEB-INF/classes/org/sakaiproject/this/class/file.properties
    istream = curClass.getResourceAsStream(fileName);
    if (istream != null) return istream;

    // Loading from the webapp class at the root
    // tomcat/webapps/sakai-webapp/WEB-INF/classes/file.properties
    istream = curClass.getResourceAsStream("/" + fileName);
    return istream;
  }
 private void copyCustomCssIfDefined() {
   final Class<?> cssClass = customCssPackage();
   final String customCss = customCss();
   if (cssClass == null || customCss == null) {
     return;
   }
   final InputStream cssInputFile = cssClass.getResourceAsStream(customCss);
   final String cssPackageName = cssClass.getPackage().getName();
   final String cssPackagePath = asPath(cssPackageName);
   final String cssOutputFileName =
       StringUtils.combinePaths(outputDir(), cssPackagePath, customCss);
   try {
     final ByteArrayOutputStream baos = new ByteArrayOutputStream();
     IoUtils.copy(cssInputFile, baos);
     if (baos.size() > 0) {
       IoUtils.copy(
           new ByteArrayInputStream(baos.toByteArray()), new FileOutputStream(cssOutputFileName));
     }
   } catch (final IllegalArgumentException e) {
     System.err.printf("failed to copy custom CSS to '%s'\n", customCss, cssOutputFileName);
     return;
   } catch (final IOException e) {
     System.err.printf("failed to copy custom CSS '%s' to '%s'\n", customCss, cssOutputFileName);
     return;
   }
 }