Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Cast to JarURLInputStream when reading jar img resources

611651Nov 12 2008 — edited Nov 12 2008
  /**
   * Load a resource based on a path under the img folder
   *
   * @param path The path + name of the file to load
   * @return The stream loaded from the file system
   * @throws java.io.FileNotFoundException Indicates a failure to read the resource
   */
  protected ImageInputStream getImgRes(String path) throws IOException {
    String fullPath = imgPath + path;

    // Try to load resource from jar
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fullPath);
    if (is == null) {
      return new FileImageInputStream(new File(fullPath)); // No Jar let's Try the local FileSystem
    } else {
      return null;
    }
  }
I've written this function so It would return an ImageInputStream when loading images from a jar.
But I get an sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream when loading from a Jar.

So I wanted to do a instanceof test and downcast it:
if(is instanceof JarURLConnection) {
// try to get an ImageInputStream out of it
}
but I'm stuck at the instanceof, they are 'inconvertable types' I don't understand the method returns InputStream
but JarURLConnection doesn't extends it.
    public InputStream getResourceAsStream(String name) {
How can I test if I have a JarURLConnection and how would I retrieve an ImageInputStream out of it?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 10 2008
Added on Nov 12 2008
2 comments
1,253 views