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

java.io.LineNumberInputStream.mark/reset methods work wrong

XMLWordPrintable

    • 1.2beta3
    • sparc
    • solaris_1, solaris_2.5
    • Verified



      Name: saC57035 Date: 12/01/96


      The java.io.LineNumberInputStream.mark/reset methods do not save/restore current
      stream state correctly.


      Here is the example demonstrating this bug:

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

      public class Test {

      public static void main( String argv[] ) {
       byte[] data = {13,20,30};
       int b1,b2;
       LineNumberInputStream in = new LineNumberInputStream(new MarkByteArrayInputStream(data));
       try {
         in.read();
         in.mark(100);
         b1 = in.read();
         in.reset();
         b2 = in.read();
         System.out.println("The following bytes should be equal: "+b1+" and "+b2);
       } catch (Throwable e) {
         System.out.println("Unexpected "+e+" is thrown");
       }
      }

      }

      class MarkByteArrayInputStream extends ByteArrayInputStream {
        int markpos = -1;

        public MarkByteArrayInputStream(byte[] b) { super(b); }

        public void mark(int readlimit) {
        markpos = pos;
       }

       public void reset() {
        if(markpos == -1) System.exit(1);
        pos = markpos;
        markpos = -1;
       }

      }
      ---------Output from the test---------------------
      The following bytes should be equal: 20 and 30
      --------------------------------------------------


      As seen from source code, the state of the LineNumberInputStream is
      determined by three variables - enclosed stream `in',
      current line number `lineNumber' and look-ahead byte `pushBack'.
      But mark/reset pair saves/restores only states of `in' and
      `lineNumber' and discards the value of `pushBack'.
        
      ======================================================================

      gregory.graham@Canada 1997-10-23
      More information from BugID= 1238945 as well suggested fix (see Suggested Fix space)
      If a LineNumberInputStream has just read a \\r character not followed by
      a \\n character, and the mark method is then called, a subsequent reset
      operation will fail to reset the stream to the correct state. The character
      that followed the \\r character will not be re-read.

      Also, if a LineNumberInputStream has just read a \\r character not followed by
      a \\n character, and the reset method is then called, that reset
      operation will fail to reset the stream to the correct state. The character
      that followed the \\r character will be read before the characters to be re-read
      are returned,

      In both cases, the problem is caused by the fact that the pushback variable is not -1.
      ===============================================================

            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: