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

(cs) InputStreamReader repeats characters after socket timeout (regression)

XMLWordPrintable

    • rc1
    • generic, x86
    • generic, solaris_8, windows_95



      Name: nt126004 Date: 11/01/2001


      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)


      I have an InputStreamReader wrapped around a socket. If a read is attempted when
      there is no waiting data, the socket throws a SocketTimeoutException. However,
      after this error the InputStreamReader rereads data previously captured. If more than
      one char is written and later read, always the first char is repeated. Using version
      1.3.1 a java.io.InterruptedIOException is thrown correctly. However, this exception
      is not thrown using 1.4.

      This occurs on 1.4beta2 & 3 on linux, and also on Windows and Solaris.

      SAMPLE SOURCE CODE

      import java.net.*;
      import java.io.*;

      public class Test {
        public static void main(String[] args) {
          try {
            new ServerThread().start();
            waitABit(1000);
            Socket s = new Socket("127.0.0.1", 22222);
            Writer w = new OutputStreamWriter(s.getOutputStream());
            w.write("ab");
            w.flush();
          } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
          }
        }

        public static class ServerThread extends Thread {
          private ServerSocket serverSocket;
          public void run() {
            try {
              serverSocket = new ServerSocket(22222);
              Socket s = serverSocket.accept();
              waitABit(300); // let the client send its byte

              s.setSoTimeout(150);
              Reader r = new InputStreamReader(s.getInputStream());
                  
              System.out.println(r.ready()); //true
              System.out.println(r.read()); //97
              System.out.println(r.read()); //98
              System.out.println(r.ready()); //false
              try {
                System.out.println(r.read()); //false
              } catch (InterruptedIOException e) {
                System.out.println("Expected IOException");
              }
              System.out.println((s.getInputStream().available())); // 0, as expected

              System.out.println(r.ready()); //true !!!!!!!!!!!!
              System.out.println(r.read()); //97 !!!!!!!!!!!!
            } catch (Exception e) {
              e.printStackTrace();
            }
            System.exit(0);
          }
        }

        private static Object lock = new Object();
        public static synchronized void waitABit(int millisec) {
          synchronized(lock) {
            try {
              lock.wait(millisec);
            } catch (InterruptedException e) {
              //ignore
            }
          }
        }
      }

      Release Regression From : 1.3.1
      The above release value was the last known release where this
      bug was knwon to work. Since then there has been a regression.

      (Review ID: 134828)
      ======================================================================

            mr Mark Reinhold
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: