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

Console.readPassword is not able to read charactars in echo off mode

    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

      Here System.in is set to FileInputStream(FileDescriptor.in), wrapped the same in InputStreamReader and opened new channle for System.in.
        When console.readPassword() follows ReadableByteChannel.read(bytebuffer.allocate(6)), it reads the characters left in the Buffer which are visible in command prompt.
        But when console.readPassword() follows InputStreamReader.read(ch[],int,len) , we can type for the password in echo off mode, even though characters are left in buffer.This should be the behaviour for previous case.

        Please see the version,code and Result.
      <version>
      java version "1.6.0-tbell_2005_11_29.17.38.41"
      Java(TM) 2 Runtime Environment, Standard Edition (build1.6.0-tbell_2005_11_29.17.38.41-b333)
      Java HotSpot(TM) Client VM (build 1.6.0-rc-b61, mixed mode)
      </version>
      <Code>
      import java.io.Console;
      import java.io.InputStreamReader;
      import java.io.FileDescriptor;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.nio.channels.Channels;
      import java.nio.channels.ReadableByteChannel;
      import java.nio.ByteBuffer;
         class TestConsole {
      public static void main(String... args) throws IOException {

      ByteBuffer bb = ByteBuffer.allocate(5);
      char ch1[]=null;
      char ch2[]=new char[5];
      char ch3[]=null;
      FileInputStream fis = new FileInputStream(FileDescriptor.in);
                  System.setIn(fis);

      InputStreamReader isr = new InputStreamReader(System.in);
      ReadableByteChannel rbc = Channels.newChannel(System.in);

      Console con = System.console();
      if ( con != null) {
                  //Test1
      con.printf("input1 :");
      rbc.read(bb);
      con.printf("Password1 :");
      ch1=con.readPassword();
                  //Test2
                  con.printf("\ninput2 :");
      isr.read(ch2,0,5);
      con.printf("Password2 :");
      ch3=con.readPassword();

      con.format("\n\n\nOutput:\ninput1 :%s \nPassword1 :%s\n",new String(bb.array()),new String(ch1));
      con.format("input2 :%s \nPassword2 :%s",new String(ch2),new String(ch3));
      }

      }
         }
      </Code>
      <Output>
      C:\work>java TestConsole
      input1 :User Name
      Password1 :

      input2 :User Name
      Password2 :

      Output:
      input1 :User
      Password1 :Name
      input2 :User
      Password2 :Password
      </Output>

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: