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

java.io.InputStream.skip(n) works wrong with negative n

XMLWordPrintable

    • 1.2beta2
    • x86, sparc
    • solaris_2.5, windows_95, windows_nt
    • Not verified

      This bug was found by St.Petersburg Java SQE team (by Stanislav Avzan).

      The java.io.InputStream.skip(long n) method works wrong with negative n,
      or, to be more exact, if (int) n is negative.

      It throws NegativeArraySizeException in this case.
      But Java Language Specification doesn't mention this possibility.
      It states very briefly:
      "22.3.4 public long skip(long n) throws IOException

      The general contract of skip is that it makes an attempt to skip over n
      bytes of data from the input stream, discarding the skipped bytes.
      However, it may skip over some smaller number of bytes, possibly
      zero. This may result from any of a number of conditions; reaching end
      of file before n bytes have been skipped is only one possibility. The
      actual number of bytes skipped is returned."

      Here is the example demonstrating the bug:
      ----Test.java---------------------
      import java.io.*;

      public class Test {

      public static void main( String argv[] ) {

       MyInputStream in = new MyInputStream();
       long skipped;
       try {
         skipped = in.skip(-23);
         System.out.println("0 bytes should be skipped: "+skipped);
       } catch (IOException e) {
         System.out.println("IOException is thrown - possible result");
       } catch (Throwable e) {
         System.out.println("Unexpected "+e+" is thrown!");
       }
      }

      }

      class MyInputStream extends InputStream {
      public int read() { return 0; }
      public int available() { return 0; }
      }
      -----Output------------------------
      Unexpected java.lang.NegativeArraySizeException is thrown!
      ----------------------------------



      Name: saC57035 Date: 12/02/96


      The similar bug is found in LineNumberInputStream as well.
      Attention please! All these bugs aren't fixed in 1.1 release!


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

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: