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

fp.bugs 2532: SequencedInputStream.read() does not skip to next input stream

XMLWordPrintable

    • 1.1
    • x86
    • windows_nt
    • Not verified

      ###@###.### (Rosanna Lee)

      All platforms: SequencedInputStream.read(byte[], int, int) does not switch to next input
      stream when it reaches EOF on stream 1.

      Steps to reproduce:
      Save the following code to TwoRead.java
      Compile and run TwoRead.java
      Once the code has completed, open Testthis.txt in a text editor. There
      should be two copies of the source code.
      // Note: I'm including comments from Rosanna Lee on a suggested fix.

      /* Tests sequenced input stream */

      import java.io.*;

      class TwoRead
      {

          public static void main(String args[])
          {
             // locals
             int iBufSize = 1024;
             boolean bflag = true;
             FileInputStream is1;
             FileInputStream is2;
             FileOutputStream os1;
             SequenceInputStream sis;

             // open streams
             try
             {
                is1 = new FileInputStream("TwoRead.java");
                is2 = new FileInputStream("TwoRead.java");
                os1 = new FileOutputStream("Testthis.txt");
             

                byte myBuffer[] = new byte[iBufSize];

                // create SequencedInputStream
                sis = new SequenceInputStream(is1, is2);

                // read through streams
                for (int i = 0; iBufSize > 0; i += myBuffer.length)
                {
                   iBufSize = sis.read(myBuffer, i, myBuffer.length);
                   os1.write(myBuffer, i, iBufSize);
                }

                is1.close();
                is2.close();
                os1.close();
             }
             finally { return; }
          }
      }

      /* Comments

      From daemon@java (0000-Admin(0000))
      Newsgroups: fp.bugs
      Subject: Unknown bug report
      Date: 13 Nov 1995 20:44:53 -0800

      >From: ###@###.### (Rosanna Lee)
      This does not look like form output to me.



      There is a problem with the following method

      int read(byte buf[], int pos, int len)

      When you create a SequenceInputStream using 2 streams
      and use this read() to read, you get a 'ArrayIndexOutOfBounds'
      exception when read() is starting on the second stream.

      In the implementation of read():

      1 public int read(byte buf[], int pos, int len) throws IOException {
      2 if (in == null) {
      3 return -1;
      4 int n = in.read(buf, pos, len);
      5 if (n <= 0) {
      6 nextStream();
      7 return read(buf, pos + n, len - n);
      8 }
      9 return n;
      10 }

      the problem is line 7. When EOF is encountered, n is -1.
      If the input to read was

      read(mybuf, 0, mybuf.length)

      then, line 7 would be trying to index on -1, which raises
      the exception. (len - n is also a problem).

      The fix is to change 7 to use the same signature as line 4
      (assuming that the -1 indicates that nothing has been read
      and EOF has been reached).

      -Rosanna

      */

      Doesn't appear to be fixed in 1.0.2...

            brenaudsunw Benjamin Renaud (Inactive)
            kasmithsunw Kevin Smith (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: