JDK-6456667 : FileNotFoundException closes all InputStreams for a specific jar file
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6
  • Priority: P2
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-08-03
  • Updated: 2011-02-16
  • Resolved: 2006-08-04
Related Reports
Duplicate :  
Description
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.

Comments
EVALUATION See Evaluation notes dated 2006-08-04 in 6449504. Undoing the changes to 6212146 allows the testcase given in this bug's Description to pass.
04-08-2006

EVALUATION Appears to be the same problem as reported in 6449504, with a smaller testcase :-) Works in 6.0-b77, fails in b.0-b78. There are no changes to any of the zip code in the stack trace from b77 to b78, but there *are* to BufferedInputStream, hence change of subcategory.
03-08-2006