public String readPassword ( ) { this . before = "" ; this . after = "" ; terminal . formatln ( "%s: " , message ) ; try { for ( ; ; ) { Char c = terminal . read ( ) ; if ( c == null ) { throw new IOException ( "End of input." ) ; } int ch = c . asInteger ( ) ; if ( ch == Char . CR ) { return before + after ; } if ( ch == Char . DEL || ch == Char . BS ) { // backspace... if ( before . length ( ) > 0 ) { before = before . substring ( 0 , before . length ( ) - 1 ) ; printInputLine ( ) ; } continue ; } if ( c instanceof Control ) { if ( c . equals ( Control . DELETE ) ) { if ( after . length ( ) > 0 ) { after = after . substring ( 1 ) ; } } else if ( c . equals ( Control . LEFT ) ) { if ( before . length ( ) > 0 ) { after = "" + before . charAt ( before . length ( ) - 1 ) + after ; before = before . substring ( 0 , before . length ( ) - 1 ) ; } } else if ( c . equals ( Control . HOME ) ) { after = before + after ; before = "" ; } else if ( c . equals ( Control . RIGHT ) ) { if ( after . length ( ) > 0 ) { before = before + after . charAt ( 0 ) ; after = after . substring ( 1 ) ; } } else if ( c . equals ( Control . END ) ) { before = before + after ; after = "" ; } else { // Silently ignore unknown control chars. continue ; } printInputLine ( ) ; continue ; } if ( ch == Char . ESC || ch == Char . ABR || ch == Char . EOF ) { throw new IOException ( "User interrupted: " + c . asString ( ) ) ; } if ( ch < 0x20 ) { // Silently ignore unknown ASCII control characters. continue ; } before = before + c . toString ( ) ; printInputLine ( ) ; } } catch ( IOException e ) { throw new UncheckedIOException ( e ) ; } }
Read password from terminal .