JDK-4454092 : FileInputStream.skip( long ) does not stop at EOF
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.2.0,1.3.0,5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_nt,windows_2000
  • CPU: generic,x86
  • Submitted: 2001-05-03
  • Updated: 2017-05-16
  • Resolved: 2004-09-20
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
5.0 tigerFixed
Related Reports
Duplicate :  
Duplicate :  
Description
Name: krC82822			Date: 05/03/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

When the skip(long) method is used from any position in the stream with a skip
value that is larger than the available bytes in the stream, the skip method
skips beyond EOF and returns the same value that was given to it.
See code below.

There are several related bugs on the Bug Parade. 
However, # 4178064, the closest match,
appears to restrict the problem to [what happens]
once the EOF has already been reached.

The scope of the problem is larger than that.

Also of note: the workaround for that bug
does not help in the situation that I demonstrate.
I think it would be helpful for others to have access to
the workaround I have provided.

========================================================

import java.io.*;

public class TestFileInputStream
{
    public static void main(String argv[])
    {
        try
        {
            //Open a FileInputStream for a "small" file
            FileInputStream f = new FileInputStream("c:\\Test.txt");

            //Try skipping way past the end of file.
            //This call should return the number of bytes in the file
            //but instead returns 500000, which according to the contract
            //for InputStream, may/should return less if the end of file is
            //reached
            long skipped = f.skip(500000);
            System.out.println("Skipped " + skipped + " bytes");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

=================================================


(Review ID: 123668) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b18 VERIFIED IN: 1.5.0_01
22-09-2004

WORK AROUND Name: krC82822 Date: 05/03/2001 ======================================================== import java.io.*; public class TestFileInputStreamWithFix { public static void main(String argv[]) { try { //Open a FileInputStream for a "small" file FileInputStream f = new FileInputStream("c:\\Test.txt"); // Only skip the value you want to skip if there is at least that // many bytes available long bytesToSkip = 500000; long skipped = 0; if ( bytesToSkip >= f.available() ) { skipped = f.skip(f.available()); } else { skipped = f.skip(bytesToSkip); } System.out.println("Skipped " + skipped + " bytes"); } catch(Exception e) { e.printStackTrace(); } } } ================================================= ======================================================================
22-09-2004

EVALUATION Clearly a bug but compatibility may prevent a change at this point. ###@###.### 2002-05-09 Will attempt to fix in Tiger. ###@###.### 2002-05-09 The spec has been updated to reflect the behavior of skip(long) in these circumstances. This behavior will be left unchanged to preserve compatibility, it is no longer considered a bug. I submitted bug 4914619 to tck because presumably the test must be changed. ###@###.### 2003-08-28
28-08-2003