RFR JDK-8167461: jshell tool: Scanner#next() hangs tool
Jan Lahoda
jan.lahoda at oracle.com
Wed Oct 12 19:41:43 UTC 2016
Hello,
Bug:
https://bugs.openjdk.java.net/browse/JDK-8167461
The problem is that this sequence of snippets is not working properly
(won't read the System.in properly/seemingly hangs):
jshell> Scanner input = new Scanner(System.in);
jshell> int x = input.nextInt();
The reason is that the PipeInputStream (which is used to pass data
received by the remote agent from the socket to System.in) does not
override the read(byte[], int, int) method. So the default
implementation will be used, which waits until all the requested data
are read. Scanner wrap s the System.in with InputStreamReader, and that
appears to read (up to) 8192 bytes into a buffer, but the user typically
does not type so many characters. The solution is to override the
read(byte[], int, int) method, and return only bytes we have buffered
(except the method will wait for the first byte).
Webrev:
http://cr.openjdk.java.net/~jlahoda/8167461/webrev.00/
Also, more general question related to the System.in handling: unused
input for one snippet is not passed to following snippets - is that OK,
or do we need to discuss changing that? I.e.:
jshell> int x = input.nextInt();
1 1
x ==> 1
//the other "1" is thrown away
jshell> int x = input.nextInt();
2
x ==> 2
//the user had to type additional input.
Thanks,
Jan
More information about the kulla-dev
mailing list