JDK-4071281 : java.io.BufferedReader.readLine: Does not work well with System.in on Win95
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1.3,1.1.4,1.1.5,1.1.6,1.1.7,1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic,windows_95
  • CPU: generic,x86
  • Submitted: 1997-08-12
  • Updated: 2002-05-15
  • Resolved: 2002-05-15
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Description

Name: rlT66838			Date: 08/12/97


import java.io.BufferedReader;
import java.io.InputStreamReader;
class Test {
   public static void main(String args[]) throws Throwable {
      BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in));
      String s;
      while ((s = inReader.readLine()) != null) System.out.println(s);
   }
}
Then, typing in "Line 1"..."Line 9"
should result in duplicates e.g.
Line 1 // input
Line 1 // output
Line 2 // input
Line 2 // output
etc. but they come out as e.g.
Line 1 // input
Line 2 // input
Line 1 // output
Line 2 // output
(this varies?!)

======================================================================
ronan.mandel@Eng 1997-09-09
Another example of this bug
he following sample program illustrates
the problem (file - Test.java):

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Test {
	public static void main (String[] args) {
		BufferedReader buf;
		InputStreamReader inp;
		
		buf = new BufferedReader(new InputStreamReader(System.in));
		String line = "";
		int count = 1;
		
		while (!line.equals("exit")) {
			System.out.print ( count + "> ");
			System.out.flush();
			try {
				line = buf.readLine();
			}
			catch (IOException ex) {
				System.out.println (ex.getMessage());
			}
			count++;
		}
	}
}


The program basically just reads and prints 
lines typed in. If you type slowly, you get the 
expected behaviour:
c:\test>java Test
1> a
2> b
3> c
4> d
5> e
6> f
7> g
8> exit

If you enter data quicker you may get, for example,
c:\test>java Test
1> a
b
2> 3> c
4> d
e
5> 6> f
g
7> 8> exit

The input seems to be buffered, but readLine is not 
detecting the EOL until another CR-LF is sent.

This doesn't seem to happen under Solaris.
It may be another manifestation of bug #4071281.

company - Sea Change Corporation , email - ###@###.###

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

Yet another report, which unlike the previous reports asserts that
DataInputStream works fine.  -- mr@eng 8/3/1998

From: "Mark Cheyne" <###@###.###>
To: <###@###.###>
Subject: BufferedReader problem
Date: Wed, 15 Jul 1998 23:05:38 +0100
 
I am using jdk1.1.6 and have experienced problems writing programs that
form part of the study of Professor Winder and Graham Roberts' book
"Developing Java Software" (published by Wiley ISBN 0-471-97655-5). I
attach two versions of one trivial program with their compilation and test
reports. The program merely reads integers from the keyboard and repeats
them back to the screen being terminated with an input zero. 

The test results of Ex166a (using BufferedReader) are mystifying, whereas
the results from Ex166b (using DataInputStream) are as expected.

Professor Winder is the head of the Computer Department at King's College
and I first referred the problem to him as he suggests using BufferedReader
in his book. He confirms that he obtains similar results. However he has
found that both programs run as expected on the Solaris platform, and has
suggested that the problem may lie with the interface between Window95 and
the JVM.

----

import java.io.*;

class Ex166a
    {public static void main (String[] arguments) throws IOException
        {int inno=1;
         String inChars="";
         BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
         while (inno!=0)
            {inChars=in.readLine();
             inno=Integer.parseInt(inChars);
             System.out.print("Input "+inno+"\n");
            }
        }
    }

/*

****       ***** Compilation:
  
C:\King's\Java\programs\Winder>javac Ex166a.java

C:\King's\Java\programs\Winder>

****       ****    Test results
C:\King's\Java\programs\Winder>java Ex166a
23
Input 23
34
Input 34
234
Input 234
231       The following lines were not put in rapidly ! but the program seems       
345            to get stuck whatever key is struck.
643
7554
4
5
0
434
Input 231
Input 345
Input 643
Input 7554
Input 4
Input 5
Input 0

C:\King's\Java\programs\Winder>
*/

----

import java.io.*;

class Ex166b
    {public static void main (String[] arguments) throws IOException,
                                                  NumberFormatException  
        {int inno=1;
         String inChars="";
         DataInputStream in=new DataInputStream(System.in);
         while (inno!=0)
            {inChars=in.readLine();
             inno=Integer.parseInt(inChars);
             System.out.print("Input  "+inno+"\n");
            }
        }
    }

/*

****    ****     Compilation:

C:\King's\Java\programs\Winder>javac Ex166b.java -deprecation
Ex166b.java:10: Note: The method java.lang.String readLine() in class java.io.Da
taInputStream has been deprecated.
            {inChars=in.readLine();
                                ^
Note: Ex166b.java uses a deprecated API.  Please consult the documentation for a
 better alternative.
2 warnings

C:\King's\Java\programs\Winder>

****    ****      Test results:

C:\King's\Java\programs\Winder>java Ex166b
12
Input  12
234
Input  234
32345
Input  32345
12
Input  12
67
Input  67
2
Input  2
3
Input  3
4
Input  4
67890
Input  67890
1234567
Input  1234567
0
Input  0

C:\King's\Java\programs\Winder>

*/

Comments
WORK AROUND Name: rlT66838 Date: 08/12/97 1) Use the deprecated DataInputStream.readLine() 2) Don't use Windows 95 (works OK on NT 4.0) ====================================================================== With JDK 1.2, everything works fine as long as you wait for the program to start up before typing at it. -- mr@eng 9/14/1998
14-09-1998

EVALUATION This is not a bug in JDK, but a Win95 bug. I was able to reproduce it on Win95 using both the native api and the c runtime library. WinNT does not have this problem. A small test shows this problem: #include <windows.h> #include <stdlib.h> #include <stdio.h> void main() { int i = 0; char buf[256]; int length = 256; int len; for(; i < 100000000;i++) { /* do nothing, just waste some time*/ } ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, length, &len, 0); //fscanf(stdin, "%s", buf); fprintf(stderr, "GOT %s\n", buf); ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, length, &len, 0); //fscanf(stdin, "%s", buf); fprintf(stderr, "GOT %s\n", buf); ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, length, &len, 0); //fscanf(stdin, "%s", buf); fprintf(stderr, "GOT %s\n", buf); ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, length, &len, 0); //fscanf(stdin, "GOT %s", buf); fprintf(stderr, "GOT %s\n", buf); } ###@###.### 1998-05-04 While this may be a Win95 bug, we really must try to find a workaround for the JDK. -- mr@eng 8/3/1998 Further research has produced no effective workaround for this Win95 bug. Lowering priority to 4. -- mr@eng 9/14/1998
03-08-1998