JDK-4213249 : (spec) FileInputStream.available() always returns 0
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.2.0,5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic,windows_2000,windows_xp
  • CPU: generic,x86
  • Submitted: 1999-02-19
  • Updated: 2005-05-20
  • Resolved: 2005-05-20
Related Reports
Relates :  
Description

Name: dbT83986			Date: 02/19/99


There seem to be a couple of areas where the behaviour of this function, and requirements on implementations of it, is unclear.

For the abstract class InputStream, the docs say "Returns the number of bytes that can be read (or skipped over) from this input stream without
blocking by the next caller of a method for this input stream."  This implies a relationship between the value of available() and subsequent calls to
read(); however this relationship is contradicated by the later statement: "The available method for class InputStream always returns 0", which implies
available() does not always return n > 0 if read() will not block.

I think I understand what you're trying to do here: the idea is that available() is a useful "helper" function which stream clients can use to determine
whether read() might block; however rather than require stream implementors to always provide an implementation of available(), you've decided
to provide a default implementation which returns zero.  (It can't do anything more useful because it doesn't know how read() will be implemented.)
That's fine, but it needs to be clearer in the docs.  It should specifically be made clear that the user CANNOT, in general, rely on the result of
available() telling him anything that useful about subsequent calls to read().  (Or at least, that a result of zero does NOT imply read() will block or
return -1.)

My second point is that it is not clear how some of the standard InputStream subclasses override available().  It is not clear, for example, whether
FileInputStream provides a useful implementation of the function, or whether it just returns 0 too.  By trial-and-error, I've observed that
FileInputStream.available() does indeed appear to always return 0, but it's not clear whether this is a bug, or whether FileInputStream does not
in fact override InputStream.available() (or does, but just invokes the superclass implementation).
(Review ID: 47340)
======================================================================

Comments
EVALUATION The first point about the doc saying "Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream." is duplicated to 4711604. The second about FileInputStream.available() always return 0 cannot be reproduced, the test has been run on solaris with jdk 1.1.8 - 1.6 and on windows 2000 & XP with jdk 1.5 - 1.6 Here is the test: import java.io.*; public class AvailableTest { public static void main(String args[]) throws Exception { final int length = 100; final String dir = (String)System.getProperty("java.io.tmpdir"); File file = new File(dir, "available_test"); if (!file.exists()) { file.createNewFile(); } System.out.println("AvailableTest-main: tests on the file: "+file); FileOutputStream out = new FileOutputStream(file); out.write("Hello\n".getBytes()); out.write(new byte[length]); out.close(); FileInputStream in = new FileInputStream(file); System.out.println("AvailableTest-main: total bytes: "+in.available()); int i = in.read(new byte[length/2]); System.out.println("AvailableTest-main: have read bytes: "+i); System.out.println("AvailableTest-main: available now: "+in.available()); i = in.read(new byte[in.available()]); System.out.println("AvailableTest-main: have read bytes: "+i); System.out.println("AvailableTest-main: available now: "+in.available()); in.close(); try { in.available(); throw new Exception ("available should throw an exception after stream is closed"); } catch (IOException e) { System.out.println("AvailableTest-main: Got the expected IOException after closing."); } file.delete(); } } The output is : AvailableTest-main: tests on the file: /var/tmp/available_test AvailableTest-main: total bytes: 106 AvailableTest-main: have read bytes: 50 AvailableTest-main: available now: 56 AvailableTest-main: have read bytes: 56 AvailableTest-main: available now: 0 AvailableTest-main: Got the expected IOException after closing. ###@###.### 2005-05-20 11:57:03 GMT
20-05-2005