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

java.io.PushbackInputStream.available() returns incorrect number

XMLWordPrintable

    • 1.2beta2
    • x86, sparc
    • solaris_2.5, windows_nt
    • Verified



      Name: saC57035 Date: 12/03/96


      The java.io.PushbackInputStream.available() method returns incorrect
      number of bytes available.


      Here is the test demonstrating the bug:

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

      public class Test {

      public static void main( String argv[] ) {
       PushbackInputStream in = new PushbackInputStream(
                new ByteArrayInputStream(new byte[10]),5);
       try {
         System.out.println("Here must be 10: "+in.available());
         in.read();
         System.out.println("Here must be 9: "+in.available());
         in.unread(20);
         System.out.println("Here must be 10: "+in.available());
         in.unread(20);
         System.out.println("Here must be 11: "+in.available());
       } catch (Throwable e) {
         System.out.println("Test failed: unexpected "+e+" is thrown");
       }
      }

      }
      ---------Output from the test---------------------
      Here must be 10: 15
      Here must be 9: 14
      Here must be 10: 13
      Here must be 11: 12
      -------------------------------------------------

      The bug is easy to fix replacing the statement in

          public int available() throws IOException {
      return pos + super.available();
          }

      with

      return buf.length - pos + super.available();

      That's all. Thanks.


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

            dviswanasunw Deepa Viswanathan (Inactive)
            savzan Stanislav Avzan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: