JDK-4711604 : (spec) InputStream.available contract not fully specified
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.3.1,1.4.0,1.4.2_10,5.0,6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS:
    generic,solaris_7,solaris_8,solaris_9,windows_2000,windows_xp generic,solaris_7,solaris_8,solaris_9,windows_2000,windows_xp
  • CPU: generic,x86,sparc
  • Submitted: 2002-07-08
  • Updated: 2017-05-16
  • Resolved: 2005-07-22
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.
JDK 6
6 b45Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Many people continue to be confused about what exactly the available
method returns.  In many cases, available will return the complete
size of the stream, leading people to write code that calls available,
allocates a byte array of that size, and does a single read into that
byte array, assuming they will get everything in the stream.  This is
clearly not true, and the javadocs for the method could do more to
clarify this.

In addition, it's not clear whether available returns a guarantee, or
merely an estimate.  For instance, if available returns 1000, and you
attempt to read 1000 bytes, is it acceptable for the stream to return
only 995 bytes (without blocking, of course)?  I believe it is.  In
some cases it's not possible to determine exactly how many bytes can
be read without doing all the work (e.g., decoding) to return the bytes.
In such a case, it seems acceptable to return an estimate of the number
of bytes that could be returned, as long as the actual read returns the
correct number of bytes without unnecessarily blocking the caller.
Short reads must always be acceptable.

The javadocs for the available method needs to address these issues, and
probably more I haven't thought of yet.

Comments
EVALUATION The suggested update looks good. ###@###.### 2005-2-07 19:52:54 GMT
07-02-2005