JDK-4028605 : ZipInputStream.available() incorrect after entry EOF
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 1.1
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1997-01-29
  • Updated: 1999-01-15
  • Resolved: 1999-01-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.2.0 1.2fcsFixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
The ZipInputStream class does not correctly implement the available method.
This method should return 0 after EOF has been reached, as implied by the
specification of the base InputStream class (JLS 22.3.5).  After returning -1
to indicate the end of the current zip entry, the method returns what appears
to be the number of bytes remaining in the entire zip file.  This inconsistency
breaks code that checks available() after EOF.

----

import java.io.*;
import java.util.zip.*;


class ZipReady {

    public static void main(String[] args) {

	try {
	    ZipInputStream z = new ZipInputStream(System.in);
	    ZipEntry e = z.getNextEntry();
	    byte[] buf = new byte[1024];
	    int n, s = 0;

	    while ((n = z.read(buf)) != -1)
		s += n;
	    System.out.println("EOF: " + s + " bytes read, " +
			       z.available() + " bytes available");
	}
	catch (Exception x) {
	    x.printStackTrace();
	}
    }
}

----

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic FIXED IN: 1.2fcs INTEGRATED IN: 1.2fcs
14-06-2004

WORK AROUND Create an InputStream class whose available method always returns 0, or that keeps track of the EOF state and causes the method to return 0 when appropriate.
11-06-2004

PUBLIC COMMENTS The java.util.ZipInputStream class does not correctly implement the available() method. After EOF has been reached on a zip-file entry it should return zero, but instead it returns the number of bytes remaining in the zip file. To work around this bug, create an InputStream class whose available method always returns 0.
10-06-2004

EVALUATION We should really deprecate InputStream.available() in favor of a more useful and reliable mechanism. It is not always possible to know how many bytes are available. I have no choice but to return only '1' if any bytes are available since I can never know exactly how many until I've read the entry. ---- I understand that it's difficult to have ZipInputStream.available return a more accurate value than one when not at EOF, but the bug here is that it doesn't return zero once EOF has been reached. Doing this should be trivial -- you just need to keep track of whether or not you've hit EOF. It's fine to return one the rest of the time. -- mr@eng 7/22/1998 Fixed. available will return 1 before EOF and 0 after EOF. ###@###.### 1998-07-24
22-07-1998