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

ByteArrayInputStream.reset() behavior and spec. don't match re: pos field value

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • generic
    • generic

      In the ByteArrayInputStream.reset() spec it says "The value of pos is set to 0."

      Testing JDK 1.4 beta shows that the pos field is not reset to 0 at the marked position. Instead pos continues to reference the original pos (depending on the constructor used, this is either the first byte of the array or the offset of the array supplied to the constructor). Now if the spec were to be implemented as it is currently stated, all the bytes prior to the marked position (which would become the pos == 0th position) may become inaccessible. The spec is ambiguous as to whether the implementation would be required to retain accessibility to the buf[0] to buf[pos - 1] bytes or whether they could be dispensed. The only API provision I can identify that would allow access to those bytes at that point (providing they hadn't been tossed in the bit bucket) would be by using a read(byte[] b,int off,int len) with a negative number as an offset.

      The following code fragment demonstrates the fact that the pos value is not reset to zero.:


      public class TestByteArrayInputStream extends java.io.ByteArrayInputStream {
          
          public TestByteArrayInputStream(byte zbuf[]) {
              super(zbuf);
          }
         
          public int getPos() {
              return pos;
          }
          
      }

      public void test() {
          
              byte ba[] = new byte { 12, 119, 54, 24, 95, 63, 84, 106, 38, 77 } ;
              
              TestByteArrayInputStream tbais = new TestByteArrayInputStream(ba);
              
              for (int i = 0; i < 3; i++) {
                        tbais.read();
              }
              
              tbais.mark(7);
              
              for (int i = 0; i < 4; i++) {
                        tbais.read();
              }
              
              tbais.reset(); // ba[7] should now be the new pos == 0

              if (tbais.getPos() != 0) {
                  println("ByteArrayInputStream.reset() did not reset field value of ByteArrayInputStream.pos to zero. Instead the value of pos = " + tbais.getPos());
              } else {
                  println("ByteArrayInputStream.reset() reset field value of ByteArrayInputStream.pos to zero.");
              }
          }
      }

      marcus.johnson@eng 2001-04-04

            mmcclosksunw Michael Mccloskey (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: