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

java.io.RandomAccessFile.seek method works wrong with Long.MIN_VALUE and Long.MA

XMLWordPrintable

    • 1.2beta2
    • sparc
    • solaris_2.5
    • Not verified

      ==== Java Language Specification v1.0 says (22.23.5) ====
      public void seek(long pos) throws IOException
      ... An IOException is thrown if pos is less than zero or greater than the length of file ...
      ==== Here is the minimized test demonstrating the bug ====
      import java.io.*;

      public class java_io_RandomAccessFile_seek {
        public static void main( String argv[] ) {
          try {
            RandomAccessFile f=new RandomAccessFile("f","rw");
            f.seek(Long.MIN_VALUE);
            System.out.println("Exception not thrown, file pointer="+f.getFilePointer());
            f.close();
          }
          catch (IOException e) {
            System.out.println(e);
          }
        }
       
      }
      ==== Here is the output of the test ====
      Exception not thrown, file pointer=0


      Name: akC57697 Date: 03/06/98


      ==== Java Language Specification v1.0 says (22.23.5) ====
      public void seek(long pos) throws IOException
      ... An IOException is thrown if pos is less than zero or greater than the length of file ...
      ==== Here is the minimized test demonstrating the bug ====
      import java.io.*;

      public class java_io_RandomAccessFile_seek {
        public static void main( String argv[] ) {
          try {
            RandomAccessFile f=new RandomAccessFile("f","rw");
            f.seek(Long.MIN_VALUE);
            System.out.println("Exception not thrown, file pointer="+f.getFilePointer());
            f.close();
          }
          catch (IOException e) {
            System.out.println(e);
          }
        }
       
      }
      ==== Here is the output of the test ====
      Exception not thrown, file pointer=0



      "Java Platform 1.2 Beta 3" documentation says:
      "public void seek(long pos) throws IOException

           Sets the file-pointer offset, measured from the beginning of this
           file, at which the next read or write occurs. The offset may be set
           beyond the end of the file. Setting the offset beyond the end of
           the file does not change the file length. The file length will
           change only by writing after the offset has been set beyond the end
           of the file.
           Parameters:
               pos - the offset position, measured in bytes from the beginning
               of the file, at which to set the file pointer.
           Throws:
               IOException - if an I/O error occurs."


      The RandomAccessFile.seek does not work correctly with Long.MAX_VALUE.
      It should not throw IOException if offset has been set beyond the end
      of the file.


      import java.io.*;
      public class Seek {
        public static void main( String argv[] ) {
          try {
            RandomAccessFile f=new RandomAccessFile("f","rw");
            try {
             f.seek(Integer.MAX_VALUE); // Try Integer.MAX_VALUE
             System.out.println("The seek works with Integer.MAX_VALUE");
            } catch (IOException e) { System.out.println("The seek throws "+e+
                                                         " with Integer.MAX_VALUE"); }
            try {
             f.seek(Long.MAX_VALUE); // Try Long.MAX_VALUE
             System.out.println("The seek works with Long.MAX_VALUE");
            } catch (IOException e) { System.out.println("The seek throws "+e+
                                                         " with Long.MAX_VALUE"); }
            f.close();
          }
          catch (IOException e) { System.out.println("Unexpected "+e);
          }
        }
      }
      Output:
      The seek works with Integer.MAX_VALUE
      The seek throws java.io.IOException: seek failed with Long.MAX_VALUE

      (java full version "JDK-1.2beta3-K")

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

            dviswanasunw Deepa Viswanathan (Inactive)
            mgorshen Mikhail Gorshenev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: