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

java.io.SequenceInputStream.read(buf,pos,len) works wrong if len=0

XMLWordPrintable

    • 1.1
    • sparc
    • solaris_2.5
    • Not verified

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

      The java.io.SequenceInputStream.read(buf,pos,len) works wrong if
      len=0. It always skips to the end of sequence and returns -1 instead
      of returning 0.
      But Java Language Specification clearly states:
      "If len is zero, then no bytes are read and 0 is returned; otherwise, there is
      an attempt to read at least one byte. If no byte is available because the
      stream is at end of file, the value -1 is returned; otherwise, at least one byte
      is read and stored into b." (see p. 22.3.3)
      Here is the simple example:
      -----Test.java----------------
      import java.io.*;

      public class Test {

         public static void main( String[] argv ) {
           StringBufferInputStream is = new StringBufferInputStream("\0");
           StringBufferInputStream is2 = new StringBufferInputStream("\1");
           try {
             SequenceInputStream sis = new SequenceInputStream(is,is2);
             byte[] bb = new byte[5];
             int b = sis.read(bb,0,0); // either
             System.out.println("Here shouldn't be -1: "+b);
           } catch(Throwable e) {
             System.out.println("Test failed: unexpected <"+e+"> thrown");
           }
         }
      }
      -----Output-------------------
      Here shouldn't be -1: -1
      ------------------------------

      The error could be semply seen from source code:

          public int read(byte buf[], int pos, int len) throws IOException {
      if (in == null) {
      return -1;
      }
      int n = in.read(buf, pos, len);
      if (n <= 0) {
      nextStream();
      return read(buf, pos, len);
      }
      return n;
          }

      The correction would be as below:

        int n = in.read(buf, pos, len);
      ! if (n < 0) {
      nextStream();



            fyellinsunw Frank Yellin (Inactive)
            mgorshen Mikhail Gorshenev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: