< prev index next >

src/java.base/share/classes/java/io/Console.java

Print this page

        

@@ -309,13 +309,14 @@
     */
     public char[] readPassword(String fmt, Object ... args) {
         char[] passwd = null;
         synchronized (writeLock) {
             synchronized(readLock) {
-                boolean echoWasOn;
                 try {
-                    echoWasOn = echo(false);
+                    echoWasOn = echo0();
+                    restoreEchoOnShutdown = true;
+                    echo(false);
                 } catch (IOException x) {
                     throw new IOError(x);
                 }
                 IOError ioe = null;
                 try {

@@ -330,10 +331,12 @@
                     } catch (IOException x) {
                         if (ioe == null)
                             ioe = new IOError(x);
                         else
                             ioe.addSuppressed(x);
+                    } finally {
+                        restoreEchoOnShutdown = false;
                     }
                     if (ioe != null)
                         throw ioe;
                 }
                 pw.println();

@@ -370,10 +373,12 @@
     private Writer out;
     private PrintWriter pw;
     private Formatter formatter;
     private Charset cs;
     private char[] rcb;
+    private boolean restoreEchoOnShutdown;
+    private boolean echoWasOn;
     private static native String encoding();
     /*
      * Sets the console echo status to {@code on} and returns the previous
      * console on/off status.
      * @param on    the echo status to set to. {@code true} for echo on and

@@ -384,11 +389,10 @@
     /*
      * Returns the current console echo on/off status.
      * @return true if the cosole echo is on
      */
     private static native boolean echo0() throws IOException;
-    private static boolean echoOn;
 
     private char[] readline(boolean zeroOut) throws IOException {
         int len = reader.read(rcb, 0, rcb.length);
         if (len < 0)
             return null;  //EOL

@@ -538,12 +542,12 @@
                 .registerShutdownHook(0 /* shutdown hook invocation order */,
                     false /* only register if shutdown is not in progress */,
                     new Runnable() {
                         public void run() {
                             try {
-                                if (cons != null)
-                                    echo(echoOn);
+                                if (cons != null && cons.restoreEchoOnShutdown)
+                                    echo(cons.echoWasOn);
                             } catch (IOException x) { }
                         }
                     });
         } catch (IllegalStateException e) {
             // shutdown is already in progress and console is first used

@@ -589,12 +593,7 @@
         reader = new LineReader(StreamDecoder.forInputStreamReader(
                      new FileInputStream(FileDescriptor.in),
                      readLock,
                      cs));
         rcb = new char[1024];
-        try {
-            echoOn = echo0();
-        } catch (IOException x) {
-            echoOn = true;
-        }
     }
 }
< prev index next >