The available call is only useful for determining if a call will block in some subclasses. It can not, and should not, always claim to be returning a number of bytes that can be read.
Name: jl125535 Date: 11/13/2003
Here is a bug report with one possible interpretation of the current specification.
<report>
FULL PRODUCT VERSION :
java version "1.3.1.01-release"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1.01-release-010816-12:37)
Java HotSpot(TM) Server VM (build 1.3.1 1.3.1.01-release-010816-13:34-PA_RISC2.0 PA2.0, mixed mode)
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Windows 2000 sp3, HP-UX B.11.00
A DESCRIPTION OF THE PROBLEM :
The return value of FileInputStream.available() always indicates that the entire file can be read without blocking. This is true for every file on the file system, including very large files and files accessed via slow network links.
The value returned from available should reflect the number of bytes that can be read *without blocking*. It is not beleivable that every single file on the whole file system can be read in its entirety without having to block at all, therefore the number returned should not always be equal to the length of the file.
I have tested this on Win2k and on HP-UX, using JDK versions 1.3.1, 1.4.1 and 1.4.2. It is 100% consistent.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
These instructions assume that you have compiled the supplied java class and the output has gone into the current directory, so that there is a subdirectory called 'test' in the current directory and it contains a file called 'AvailableTest.class'.
java -classpath . AvailableTest <filename> [<filename> [...]]
Make sure you pick a really huge file that is accessed across a realy slow network link, so that you can be sure that reading the entire file will block at some point.
You might want to reboot your computer before you execute the test to ensure that the file you choose is not already cached in RAM.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The test should display the text "If this message ever gets printed on the console, this bug can be considered fixed!".
ACTUAL -
The test will display the text "FileInputStream.available() claims the entirety of file <filename> can be read without blocking <file length> bytes)"
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class AvailableTest
{
public static void main(String[] args) throws IOException
{
if (args.length == 0)
{
System.out.println(
"You must specify a filename on the command line.");
System.exit(1);
}
for (int i = 0; i < args.length; i++)
{
FileInputStream fin = new FileInputStream(args[i]);
try
{
long available = fin.available();
long length = new File(args[i]).length();
if (available == length)
{
System.out.println(
"FileInputStream.available() claims the entirety"
+ " of file " + args[i] + " can be read without"
+ " blocking (" + available + " bytes)");
}
else
{
System.out.println("this bug can be considered fixed!");
}
}
finally
{
fin.close();
}
}
}
}
---------- END SOURCE ----------
</report>
(Review ID: 225449)
======================================================================