Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6402006

FileInputStream.available() returns negative values when reading a large file

XMLWordPrintable

    • b123
    • x86
    • windows_xp
    • Verified

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]


      A DESCRIPTION OF THE PROBLEM :
      When reading a large file using FileInputStream, the available() method returns negative values after a certain point. For example, I created a file 7,405,576,182 bytes long, and seeked 3,110,608,892 bytes into the file. The available() method returned -5. I tried this on 3 different Windows boxes with different versions of Java and I found the exact same problem on all of them.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      * Create a file 7,405,576,182 bytes long.
      * Open the file using FileInputStream.
      * Seek 3,110,608,892 bytes into the file.
      * Call the available() method and check the result.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect a positive number.
      ACTUAL -
      The funtion returned -5.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      // WARNING: This program creates a 6.9 GB file. Ensure you have sufficient space before running it.

      import java.io.*;

      public class Test
      {
      public static void main(String[] args) throws IOException
      {
      File file = new File("x");

      RandomAccessFile raf = new RandomAccessFile(file, "rw");
      raf.seek(7405576182L);
      raf.write(1);
      raf.close();

      FileInputStream fis = new FileInputStream(file);
      fis.skip(3110608882L);
      System.out.println(fis.available());
      fis.skip(10L);
      System.out.println(fis.available());
      fis.close();

      file.delete();
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I worked around this problem by creating a wrapper class for FileInputStream with a different implementation of the available() method which returned the number of bytes between the current file pointer and the end of the file.

            mchung Mandy Chung
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: