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

BufferedReader.ready() is not detecting the removal of data off the stream.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • core-libs
    • x86
    • linux



      Name: stC104175 Date: 06/26/2000


      Java HotSpot(TM) Client VM (build 1.3.0beta-b04, mixed mode)

      The problem lies with the implementation of the BufferedReader's implementation
      of ready. In the following code:
      ************************************
      import java.io.*;
      import java.util.*;
      import java.lang.*;
      import java.net.*;

      class NetworkInterface implements Runnable
      {
        Vector inVec;
        Vector outVec;
        Object inVecLock;
        Object outVecLock;
        ServerSocket ss;
        Socket socket;
       
        BufferedReader in;
        PrintWriter out;

        public NetworkInterface( Vector inVec, Vector outVec, Object ilock, Object
      olock )
      throws IOException
        {
          this.inVec = inVec;
          this.outVec = outVec;
          inVecLock = ilock;
          outVecLock = olock;

          ss = new ServerSocket( 3333 );
          socket = ss.accept();

          in = new BufferedReader( new InputStreamReader( socket.getInputStream() ));
          out = new PrintWriter( socket.getOutputStream(), true );
          new Thread( this ).start();
        }

        public void run()
        {
          while( true )
          {
            try
            {
              synchronized( outVecLock )
              {
                if ( ! outVec.isEmpty() )
                {
      out.println( outVec.remove( 0 ) );
                }
              }
              if ( in.ready() )
              {
      synchronized( inVecLock )
      {
      inVec.add( in.readLine() );
      }
              }
              try
              {
      Thread.currentThread().sleep( 500 );
              } catch ( InterruptedException e ) {}
            } catch ( Exception e )
            {
      System.err.println( " ERROR: " + e.getMessage() );
            }
          }
        }
      }

      class Messages implements Runnable
      {
        Vector inVec;
        Vector outVec;
        Object inVecLock;
        Object outVecLock;

        public Messages( Vector inVec, Vector outVec, Object iLock, Object oLock )
        {
          this.inVec = inVec;
          this.outVec = outVec;
          inVecLock = iLock;
          outVecLock = oLock;

          new Thread( this ).start();
        }

        public void run()
        {
          int i = 0;
          while( true )
          {
            synchronized( inVecLock )
            {
              if ( ! inVec.isEmpty() )
              {
      System.out.println( "R: " + inVec.remove( 0 ) );
              }
            }
            System.out.println( "S: " + i );
            synchronized( outVecLock )
            {
      outVec.add( new Integer( i ));
            }
            i++;
            try
            {
              Thread.currentThread().sleep( 1000 );
            } catch ( InterruptedException e ) {}
          }
        }
      }

      public class bug
      {

        public static void main( String[] args )
      throws Exception
        {
          Vector in = new Vector();
          Vector out = new Vector();
          Object iLock = new Object();
          Object oLock = new Object();

          NetworkInterface n = new NetworkInterface( in, out, iLock, oLock );

          Messages m = new Messages( in, out, iLock, oLock );
        }
      }
      *********************************************

      once the NetworkInterfaceThread receives a packet on the bufferedreader stream,
      all subsequent calls of ready() return true.
      (Compiled and running under JDK1.2) output of the program

      S: 0
      S: 1
      S: 2
      S: 3
      R: abc /* send via a telnet session */
      S: 4
      S: 5
      S: 6
      S: 7
      S: 8
      (killed the program otherwise would have continued running).

      (Compiled and running under JDK1.3) Output of the program
      S: 0
      S: 1
      S: 2
      S: 3
      S: 4
      S: 5
      R: abc /*again sent via a telnet session */
      S: 6

      The program hangs at this point because now that it's read one packet (abc in
      this case) on the subsequent loop it's check of ready() has returned true and is
      expecting something to read off the network and is blocked. As is demonstrated
      by the following output

      S: 0
      S: 1
      S: 2
      S: 3
      S: 4
      S: 5
      R: abc
      S: 6
      R: lmn
      S: 7
      R: ghi
      S: 8
      (Review ID: 106509)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            stompkinsunw Sean Tompkins (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: