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

Console.readLine() does not read the input stream left in the input stream buffer

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Not an Issue
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      java.io.Console.readLine() does not read the characters left in the input stream buffer,after read
      with BufferedReader( Console.reader() ).read(char[],int off,int len).

      See the following version,code
      <version>
      C:\work>java -version
      java version "1.6.0-auto"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-auto-284)
      Java HotSpot(TM) Client VM (build 1.6.0-ea-b56, mixed mode)
      </version>
      <code>
      import java.io.*;
      class TestConsole3 {
      public static void main(String[] args) {
      try{
      Console con =System.console();
      String s1=null;
      char ch[]=new char[20];
      if (con != null) {
      Reader reader = con.reader();
      BufferedReader br = new BufferedReader(reader);
      con.printf("Testing with BufferedReader.read() :%n");
      con.printf("enter a line: ");
      int n=br.read(ch,0,10);
      con.printf("characters read with br.read(char[],int,int) : %s %n",new String(ch));
      con.printf("Now Reading with console.readLine() : ");
      s1=con.readLine();
      con.printf("stream Read with console.readLine() is: %s ", s1 );

      }
      }catch(IOException e){
      e.printStackTrace();
      }
      }
      }
      </code>
      <Result>
      D:\work\pp>java TestConsole3
      Testing with BufferedReader.read() :
      enter a line: Sun Rises In The East
      characters read with br.read(char[],int,int) : Sun Rises
      Now Reading with console.readLine() : hello java
      stream Read with console.readLine() is: hello java
      </Result>
         Here Console.readLine() discards the input stream 'In The East',opens new input stream.But the behavior is as expected when we replace BufferedReader with PushbackReader in the above program.
      See the following code and result.

      <code>
      import java.io.*;
      class TestConsole4 {
      public static void main(String[] args) {
      try{
      Console con =System.console();
      String s1=null;
      char ch[]=new char[20];
      if (con != null) {
      Reader reader = con.reader();
      PushbackReader pbr = new PushbackReader(reader);
      con.printf("Testing with PushbackReader.read() :%n");
      con.printf("enter a line: ");
      int n=pbr.read(ch,0,10);
      con.printf("characters read with pbr.read(char[],int,int) : %s %n",new String(ch));
      con.printf("Now Reading with console.readLine() : %n");
      s1=con.readLine();
      con.printf("stream Read with console.readLine() is: %s ", s1 );

      }
      }catch(IOException e){
      e.printStackTrace();
      }
      }
      }
      </code>
      <Result>
      D:\work\pp>java TestConsole4
      Testing with PushbackReader.read() :
      enter a line: Sun Rises In The East
      characters read with pbr.read(char[],int,int) : Sun Rises
      Now Reading with console.readLine() :
      stream Read with console.readLine() is: In The East
      </Result>
        
          There is conflict in behavior of Console.readLine(), reading the input stream buffer left after read with PushbackReader(Console.reader()).read() and BufferedReader(Console.reader).read().
      This should be resolved.

        Attachments

          Activity

            People

            Assignee:
            sherman Xueming Shen
            Reporter:
            rgutupalsunw Rajendra Gutupalli (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: