JDK-6347312 : Console.readLine() throws misleading IOError after InputStreamReader(System.in).close()
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2005-11-08
  • Updated: 2010-04-02
  • Resolved: 2005-12-08
Related Reports
Relates :  
Description
The Exception is misleading and closing InputStreamReader(System.in) does not allow Console.readLine() to read a line and Console.readPassword() to read password.
For Console.readPassword(), exception message is:  IOError :java.io.IOException: The handle is invalid. See Result2:
Closing InputStreamReader(System.in) should not affect Console readLine(),readPassword() methods.
Please see the following code,Result1.
<code>
import java.io.*;
class TestClose4 {      
  public static void main(String... s){
   Console con =System.console(); 
   try{
      InputStreamReader reader = new InputStreamReader(System.in);
      reader.close();
      String st=con.readLine("%s","Enter a line");
      System.out.println(st);
   }catch(IOException e){
      e.printStackTrace();
   }catch(IOError error){
      error.printStackTrace();
   }
 }
}
</code>
<Result1>
C:\work>java TestClose4
Enter a linejava.io.IOError: java.io.IOException: No such file or directory
        at java.io.Console.readLine(Console.java:236)
        at TestClose4.main(TestClose4.java:9)
Caused by: java.io.IOException: No such file or directory
        at java.io.FileInputStream.readBytes(Native Method)
        at java.io.FileInputStream.read(FileInputStream.java:199)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
        at java.io.Console$LineReader.read(Console.java:409)
        at java.io.Console.readline(Console.java:348)
        at java.io.Console.readLine(Console.java:232)
        ... 1 more
</Result1>

<Result2>
C:\work>java TestClose4
Enter a Passwordjava.io.IOError: java.io.IOException: The handle is invalid
        at java.io.Console.readPassword(Console.java:302)
        at TestClose4.main(TestClose4.java:10)
Caused by: java.io.IOException: The handle is invalid
        at java.io.Console.echo(Native Method)
        at java.io.Console.readPassword(Console.java:299)
        ... 1 more
</Result2>

Comments
EVALUATION "The console readLine/readPassword no longer works" issue is traced in 6347283 together with their output peers. I dont think we can and really want to catch the exception and "translate" the error message to a "better" one. Closed as "will not fix".
08-12-2005

EVALUATION You say: Closing InputStreamReader(System.in) should not affect Console readLine(), readPassword() methods. I disagree. The spec for Console says that invoking close() on the objects returned by reader() will not close the underlying stream. That's not how you've aquired the reader. In your test case, the spec for Reader.close() (which is inherited by InputStreamReader.close()) is applicable: Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect. There is exactly one System.in for every VM, so it should not be surprising that any operations directed to it would fail after invoking close() in this manner. As for the specific detail message, I believe that we're simply propagating the message from the underlying OS. It may be possible to provide a slightly better error message if we could detect that the console device has been closed before we actually try to read it. However, this may not be possible since Reader does not provide any mechanism to detect whether it has been previously closed. Passing this bug along to the engineer responsible for Console to see if he has an thoughts on how we might provide a better detail message.
11-11-2005