United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4454092 : FileInputStream.skip( long ) does not stop at EOF

Details
Type:
Bug
Submit Date:
2001-05-03
Status:
Resolved
Updated Date:
2013-05-09
Project Name:
JDK
Resolved Date:
2004-09-20
Component:
core-libs
OS:
windows_nt,generic,windows_2000
Sub-Component:
java.io
CPU:
x86,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.2.0,1.3.0,5.0
Fixed Versions:
5.0 (tiger)

Related Reports
Duplicate:
Duplicate:

Sub Tasks

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
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();
        }
    }
}

=================================================
======================================================================
                                     
2004-09-22
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger

FIXED IN:
tiger

INTEGRATED IN:
tiger
tiger-b18

VERIFIED IN:
1.5.0_01


                                     
2004-09-22
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
                                     
2003-08-28



Hardware and Software, Engineered to Work Together