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

java.io.PushbackInputStream.read(b,off,len) works wrong with len

XMLWordPrintable

    • 1.2beta4
    • sparc
    • solaris_2.5
    • Verified



      Name: saC57035 Date: 12/02/96


      The java.io.PushbackInputStream.read(b,off,len) works wrong with len.
      It does not throw IOException if len is out of bounds, though
      Java Language Specification item 22.3.3 clearly states:

      "22.3.3 public int read(byte[] bytes, int offset,
      int length) throws IOException,
      NullPointerException, IndexOutOfBoundsException

      The general contract of read(b, off, len) is that it reads some
      number of bytes from the input stream and stores them into the buffer
      array b.
      An attempt is made to read as many as len bytes, but a smaller
      number may be read, possibly zero. The number of bytes actually read
      is returned as an integer.

      This method blocks until input data is available, end of file is detected,
      or an exception is thrown.

      If b is null, a NullPointerException is thrown.

      If off is negative, or len is negative, or off+len is greater than the
      length of the array b, then an IndexOutOfBoundsException is
      thrown."


      Here is the test demonstrating the bug:

      -----------------Test.java------------------------
      import java.io.*;

      public class Test {

      public static void main( String argv[] ) {
       byte[] data = {30,40,50};
       PushbackInputStream in = new PushbackInputStream(new ByteArrayInputStream(data));
       try {
         in.unread(20);
         byte[] b = new byte[10];
         in.read(b,0,-100); // negative len used
         System.out.println("Test failed: IndexOutOfBoundsException is not thrown");
       }
       catch(IndexOutOfBoundsException e) {
         System.out.println("Test passed: IndexOutOfBoundsException is thrown");
       } catch (Throwable e) {
         System.out.println("Test failed: unexpected "+e+" is thrown");
       }
      }

      }
      ---------Output from the test---------------------
      Test failed: IndexOutOfBoundsException is not thrown
      --------------------------------------------------

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

            zlisunw Zhenghua Li (Inactive)
            savzan Stanislav Avzan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: