< prev index next >

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

Print this page




 294     * @throws  IllegalFormatException
 295     *          If a format string contains an illegal syntax, a format
 296     *          specifier that is incompatible with the given arguments,
 297     *          insufficient arguments given the format string, or other
 298     *          illegal conditions.  For specification of all possible
 299     *          formatting errors, see the <a
 300     *          href="../util/Formatter.html#detail">Details</a>
 301     *          section of the formatter class specification.
 302     *
 303     * @throws IOError
 304     *         If an I/O error occurs.
 305     *
 306     * @return  A character array containing the password or passphrase read
 307     *          from the console, not including any line-termination characters,
 308     *          or {@code null} if an end of stream has been reached.
 309     */
 310     public char[] readPassword(String fmt, Object ... args) {
 311         char[] passwd = null;
 312         synchronized (writeLock) {
 313             synchronized(readLock) {
 314                 boolean echoWasOn;
 315                 try {
 316                     echoWasOn = echo(false);


 317                 } catch (IOException x) {
 318                     throw new IOError(x);
 319                 }
 320                 IOError ioe = null;
 321                 try {
 322                     if (fmt.length() != 0)
 323                         pw.format(fmt, args);
 324                     passwd = readline(true);
 325                 } catch (IOException x) {
 326                     ioe = new IOError(x);
 327                 } finally {
 328                     try {
 329                         echo(echoWasOn);
 330                     } catch (IOException x) {
 331                         if (ioe == null)
 332                             ioe = new IOError(x);
 333                         else
 334                             ioe.addSuppressed(x);


 335                     }
 336                     if (ioe != null)
 337                         throw ioe;
 338                 }
 339                 pw.println();
 340             }
 341         }
 342         return passwd;
 343     }
 344 
 345    /**
 346     * Reads a password or passphrase from the console with echoing disabled
 347     *
 348     * @throws IOError
 349     *         If an I/O error occurs.
 350     *
 351     * @return  A character array containing the password or passphrase read
 352     *          from the console, not including any line-termination characters,
 353     *          or {@code null} if an end of stream has been reached.
 354     */
 355     public char[] readPassword() {
 356         return readPassword("");
 357     }
 358 
 359     /**
 360      * Flushes the console and forces any buffered output to be written
 361      * immediately .
 362      */
 363     public void flush() {
 364         pw.flush();
 365     }
 366 
 367     private Object readLock;
 368     private Object writeLock;
 369     private Reader reader;
 370     private Writer out;
 371     private PrintWriter pw;
 372     private Formatter formatter;
 373     private Charset cs;
 374     private char[] rcb;


 375     private static native String encoding();
 376     /*
 377      * Sets the console echo status to {@code on} and returns the previous
 378      * console on/off status.
 379      * @param on    the echo status to set to. {@code true} for echo on and
 380      *              {@code false} for echo off
 381      * @return true if the previous console echo status is on
 382      */
 383     private static native boolean echo(boolean on) throws IOException;
 384     /*
 385      * Returns the current console echo on/off status.
 386      * @return true if the cosole echo is on
 387      */
 388     private static native boolean echo0() throws IOException;
 389     private static boolean echoOn;
 390 
 391     private char[] readline(boolean zeroOut) throws IOException {
 392         int len = reader.read(rcb, 0, rcb.length);
 393         if (len < 0)
 394             return null;  //EOL
 395         if (rcb[len-1] == '\r')
 396             len--;        //remove CR at end;
 397         else if (rcb[len-1] == '\n') {
 398             len--;        //remove LF at end;
 399             if (len > 0 && rcb[len-1] == '\r')
 400                 len--;    //remove the CR, if there is one
 401         }
 402         char[] b = new char[len];
 403         if (len > 0) {
 404             System.arraycopy(rcb, 0, b, 0, len);
 405             if (zeroOut) {
 406                 Arrays.fill(rcb, 0, len, ' ');
 407             }
 408         }
 409         return b;


 523                         }
 524                     }
 525                     if (eof)
 526                         return off - offset;
 527                 }
 528             }
 529         }
 530     }
 531 
 532     // Set up JavaIOAccess in SharedSecrets
 533     static {
 534         try {
 535             // Add a shutdown hook to restore console's echo state should
 536             // it be necessary.
 537             SharedSecrets.getJavaLangAccess()
 538                 .registerShutdownHook(0 /* shutdown hook invocation order */,
 539                     false /* only register if shutdown is not in progress */,
 540                     new Runnable() {
 541                         public void run() {
 542                             try {
 543                                 if (cons != null)
 544                                     echo(echoOn);
 545                             } catch (IOException x) { }
 546                         }
 547                     });
 548         } catch (IllegalStateException e) {
 549             // shutdown is already in progress and console is first used
 550             // by a shutdown hook
 551         }
 552 
 553         SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
 554             public Console console() {
 555                 if (istty()) {
 556                     if (cons == null)
 557                         cons = new Console();
 558                     return cons;
 559                 }
 560                 return null;
 561             }
 562 
 563             public Charset charset() {
 564                 // This method is called in sun.security.util.Password,


 574         writeLock = new Object();
 575         String csname = encoding();
 576         if (csname != null) {
 577             try {
 578                 cs = Charset.forName(csname);
 579             } catch (Exception x) {}
 580         }
 581         if (cs == null)
 582             cs = Charset.defaultCharset();
 583         out = StreamEncoder.forOutputStreamWriter(
 584                   new FileOutputStream(FileDescriptor.out),
 585                   writeLock,
 586                   cs);
 587         pw = new PrintWriter(out, true) { public void close() {} };
 588         formatter = new Formatter(out);
 589         reader = new LineReader(StreamDecoder.forInputStreamReader(
 590                      new FileInputStream(FileDescriptor.in),
 591                      readLock,
 592                      cs));
 593         rcb = new char[1024];
 594         try {
 595             echoOn = echo0();
 596         } catch (IOException x) {
 597             echoOn = true;
 598         }
 599     }
 600 }


 294     * @throws  IllegalFormatException
 295     *          If a format string contains an illegal syntax, a format
 296     *          specifier that is incompatible with the given arguments,
 297     *          insufficient arguments given the format string, or other
 298     *          illegal conditions.  For specification of all possible
 299     *          formatting errors, see the <a
 300     *          href="../util/Formatter.html#detail">Details</a>
 301     *          section of the formatter class specification.
 302     *
 303     * @throws IOError
 304     *         If an I/O error occurs.
 305     *
 306     * @return  A character array containing the password or passphrase read
 307     *          from the console, not including any line-termination characters,
 308     *          or {@code null} if an end of stream has been reached.
 309     */
 310     public char[] readPassword(String fmt, Object ... args) {
 311         char[] passwd = null;
 312         synchronized (writeLock) {
 313             synchronized(readLock) {

 314                 try {
 315                     echoWasOn = echo0();
 316                     restoreEchoOnShutdown = true;
 317                     echo(false);
 318                 } catch (IOException x) {
 319                     throw new IOError(x);
 320                 }
 321                 IOError ioe = null;
 322                 try {
 323                     if (fmt.length() != 0)
 324                         pw.format(fmt, args);
 325                     passwd = readline(true);
 326                 } catch (IOException x) {
 327                     ioe = new IOError(x);
 328                 } finally {
 329                     try {
 330                         echo(echoWasOn);
 331                     } catch (IOException x) {
 332                         if (ioe == null)
 333                             ioe = new IOError(x);
 334                         else
 335                             ioe.addSuppressed(x);
 336                     } finally {
 337                         restoreEchoOnShutdown = false;
 338                     }
 339                     if (ioe != null)
 340                         throw ioe;
 341                 }
 342                 pw.println();
 343             }
 344         }
 345         return passwd;
 346     }
 347 
 348    /**
 349     * Reads a password or passphrase from the console with echoing disabled
 350     *
 351     * @throws IOError
 352     *         If an I/O error occurs.
 353     *
 354     * @return  A character array containing the password or passphrase read
 355     *          from the console, not including any line-termination characters,
 356     *          or {@code null} if an end of stream has been reached.
 357     */
 358     public char[] readPassword() {
 359         return readPassword("");
 360     }
 361 
 362     /**
 363      * Flushes the console and forces any buffered output to be written
 364      * immediately .
 365      */
 366     public void flush() {
 367         pw.flush();
 368     }
 369 
 370     private Object readLock;
 371     private Object writeLock;
 372     private Reader reader;
 373     private Writer out;
 374     private PrintWriter pw;
 375     private Formatter formatter;
 376     private Charset cs;
 377     private char[] rcb;
 378     private boolean restoreEchoOnShutdown;
 379     private boolean echoWasOn;
 380     private static native String encoding();
 381     /*
 382      * Sets the console echo status to {@code on} and returns the previous
 383      * console on/off status.
 384      * @param on    the echo status to set to. {@code true} for echo on and
 385      *              {@code false} for echo off
 386      * @return true if the previous console echo status is on
 387      */
 388     private static native boolean echo(boolean on) throws IOException;
 389     /*
 390      * Returns the current console echo on/off status.
 391      * @return true if the cosole echo is on
 392      */
 393     private static native boolean echo0() throws IOException;

 394 
 395     private char[] readline(boolean zeroOut) throws IOException {
 396         int len = reader.read(rcb, 0, rcb.length);
 397         if (len < 0)
 398             return null;  //EOL
 399         if (rcb[len-1] == '\r')
 400             len--;        //remove CR at end;
 401         else if (rcb[len-1] == '\n') {
 402             len--;        //remove LF at end;
 403             if (len > 0 && rcb[len-1] == '\r')
 404                 len--;    //remove the CR, if there is one
 405         }
 406         char[] b = new char[len];
 407         if (len > 0) {
 408             System.arraycopy(rcb, 0, b, 0, len);
 409             if (zeroOut) {
 410                 Arrays.fill(rcb, 0, len, ' ');
 411             }
 412         }
 413         return b;


 527                         }
 528                     }
 529                     if (eof)
 530                         return off - offset;
 531                 }
 532             }
 533         }
 534     }
 535 
 536     // Set up JavaIOAccess in SharedSecrets
 537     static {
 538         try {
 539             // Add a shutdown hook to restore console's echo state should
 540             // it be necessary.
 541             SharedSecrets.getJavaLangAccess()
 542                 .registerShutdownHook(0 /* shutdown hook invocation order */,
 543                     false /* only register if shutdown is not in progress */,
 544                     new Runnable() {
 545                         public void run() {
 546                             try {
 547                                 if (cons != null && cons.restoreEchoOnShutdown)
 548                                     echo(cons.echoWasOn);
 549                             } catch (IOException x) { }
 550                         }
 551                     });
 552         } catch (IllegalStateException e) {
 553             // shutdown is already in progress and console is first used
 554             // by a shutdown hook
 555         }
 556 
 557         SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
 558             public Console console() {
 559                 if (istty()) {
 560                     if (cons == null)
 561                         cons = new Console();
 562                     return cons;
 563                 }
 564                 return null;
 565             }
 566 
 567             public Charset charset() {
 568                 // This method is called in sun.security.util.Password,


 578         writeLock = new Object();
 579         String csname = encoding();
 580         if (csname != null) {
 581             try {
 582                 cs = Charset.forName(csname);
 583             } catch (Exception x) {}
 584         }
 585         if (cs == null)
 586             cs = Charset.defaultCharset();
 587         out = StreamEncoder.forOutputStreamWriter(
 588                   new FileOutputStream(FileDescriptor.out),
 589                   writeLock,
 590                   cs);
 591         pw = new PrintWriter(out, true) { public void close() {} };
 592         formatter = new Formatter(out);
 593         reader = new LineReader(StreamDecoder.forInputStreamReader(
 594                      new FileInputStream(FileDescriptor.in),
 595                      readLock,
 596                      cs));
 597         rcb = new char[1024];





 598     }
 599 }
< prev index next >