Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6456667

FileNotFoundException closes all InputStreams for a specific jar file

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0-rc"
      Java(TM) SE Runtime Environment (build 1.6.0-rc-b93)
      Java HotSpot(TM) Client VM (build 1.6.0-rc-b93, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      When opening a stream from a jar: URL eg:
      new URL("jar:/blah.jar!/doesNotExist.txt").openStream()
      if the file does not exist, the zip file is closed and any streams previously opened from that jar file stop working.

      ie:
      InputStream stream1 = new URL("jar:/blah.jar!/exists.txt").openStream();
      try {
      InputStream stream2 = new URL("jar:/blah.jar!/doesNotExist.txt").openStream();
      } catch (FileNotFoundException e) {}
      stream1.read(); // throws ZipException: ZipFile closed



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a jar file called test.jar with a single file in it: test.txt (test.txt can contain any content)
      2. Compile and run the source code below in the same directory as test.jar


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The test code should successfully read from test.txt.
      ACTUAL -
      A ZipException is thrown when attempting to read from test.jar!/test.txt

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Test1 available: 16
      Exception in thread "main" java.util.zip.ZipException: ZipFile closed
              at java.util.zip.ZipFile.ensureOpenOrZipException(ZipFile.java:413)
              at java.util.zip.ZipFile.access$1100(ZipFile.java:29)
              at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:445)
              at java.util.zip.ZipFile$1.fill(ZipFile.java:230)
              at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:141)
              at java.io.FilterInputStream.read(FilterInputStream.java:116)
              at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
              at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
              at JarUrls.main(JarUrls.java:20)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      import java.net.*;

      public class JarUrls {

          public static void main(String[] args) throws Exception {
              File jarFile = new File("test.jar");
              String jarUrl = "jar:" + jarFile.toURL().toString() + "!/";
              URL test1 = new URL(jarUrl + "test.txt");
              InputStream test1Stream = new BufferedInputStream(test1.openStream());
              URL test2 = new URL(jarUrl + "test2.txt");
              try {
                  InputStream test2Stream = new BufferedInputStream(test2.openStream());
                  throw new Exception("Test 2 somehow existed.");
              } catch(FileNotFoundException e) {
                  System.err.println("Good: test2.txt doesn't exist.");
              }

              System.err.println("Test1 available: " + test1Stream.available());
              System.err.println("Test1 read: " + test1Stream.read());
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Create a class loader for the jar file and use getResourceAsStream() on the class loader to access the contents of the jar file. Manually opening the zip file and reading its contents would also work.

      Release Regression From : mustang
      The above release value was the last known release where this
      bug was not reproducible. Since then there has been a regression.

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: