< prev index next >

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

Print this page
rev 51515 : 8209937: Enhance java.io.Console - provide methods to query console width and height
Contributed-by: christoph.langer@sap.com, matthias.baesken@sap.com
   1 /*
   2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.util.*;
  29 import java.nio.charset.Charset;




  30 import jdk.internal.misc.JavaIOAccess;
  31 import jdk.internal.misc.SharedSecrets;
  32 import sun.nio.cs.StreamDecoder;
  33 import sun.nio.cs.StreamEncoder;
  34 
  35 /**
  36  * Methods to access the character-based console device, if any, associated
  37  * with the current Java virtual machine.
  38  *
  39  * <p> Whether a virtual machine has a console is dependent upon the
  40  * underlying platform and also upon the manner in which the virtual
  41  * machine is invoked.  If the virtual machine is started from an
  42  * interactive command line without redirecting the standard input and
  43  * output streams then its console will exist and will typically be
  44  * connected to the keyboard and display from which the virtual machine
  45  * was launched.  If the virtual machine is started automatically, for
  46  * example by a background job scheduler, then it will typically not
  47  * have a console.
  48  * <p>
  49  * If this virtual machine has a console then it is represented by a


 373     *
 374     * @throws IOError
 375     *         If an I/O error occurs.
 376     *
 377     * @return  A character array containing the password or passphrase read
 378     *          from the console, not including any line-termination characters,
 379     *          or {@code null} if an end of stream has been reached.
 380     */
 381     public char[] readPassword() {
 382         return readPassword("");
 383     }
 384 
 385     /**
 386      * Flushes the console and forces any buffered output to be written
 387      * immediately .
 388      */
 389     public void flush() {
 390         pw.flush();
 391     }
 392 














 393     private Object readLock;
 394     private Object writeLock;
 395     private Reader reader;
 396     private Writer out;
 397     private PrintWriter pw;
 398     private Formatter formatter;
 399     private Charset cs;
 400     private char[] rcb;
 401     private boolean restoreEcho;
 402     private boolean shutdownHookInstalled;
 403     private static native String encoding();

 404     /*
 405      * Sets the console echo status to {@code on} and returns the previous
 406      * console on/off status.
 407      * @param on    the echo status to set to. {@code true} for echo on and
 408      *              {@code false} for echo off
 409      * @return true if the previous console echo status is on
 410      */
 411     private static native boolean echo(boolean on) throws IOException;
 412 
 413     private char[] readline(boolean zeroOut) throws IOException {
 414         int len = reader.read(rcb, 0, rcb.length);
 415         if (len < 0)
 416             return null;  //EOL
 417         if (rcb[len-1] == '\r')
 418             len--;        //remove CR at end;
 419         else if (rcb[len-1] == '\n') {
 420             len--;        //remove LF at end;
 421             if (len > 0 && rcb[len-1] == '\r')
 422                 len--;    //remove the CR, if there is one
 423         }


   1 /*
   2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 

  28 import java.nio.charset.Charset;
  29 import java.util.Arrays;
  30 import java.util.Formatter;
  31 import java.util.IllegalFormatException;
  32 
  33 import jdk.internal.misc.JavaIOAccess;
  34 import jdk.internal.misc.SharedSecrets;
  35 import sun.nio.cs.StreamDecoder;
  36 import sun.nio.cs.StreamEncoder;
  37 
  38 /**
  39  * Methods to access the character-based console device, if any, associated
  40  * with the current Java virtual machine.
  41  *
  42  * <p> Whether a virtual machine has a console is dependent upon the
  43  * underlying platform and also upon the manner in which the virtual
  44  * machine is invoked.  If the virtual machine is started from an
  45  * interactive command line without redirecting the standard input and
  46  * output streams then its console will exist and will typically be
  47  * connected to the keyboard and display from which the virtual machine
  48  * was launched.  If the virtual machine is started automatically, for
  49  * example by a background job scheduler, then it will typically not
  50  * have a console.
  51  * <p>
  52  * If this virtual machine has a console then it is represented by a


 376     *
 377     * @throws IOError
 378     *         If an I/O error occurs.
 379     *
 380     * @return  A character array containing the password or passphrase read
 381     *          from the console, not including any line-termination characters,
 382     *          or {@code null} if an end of stream has been reached.
 383     */
 384     public char[] readPassword() {
 385         return readPassword("");
 386     }
 387 
 388     /**
 389      * Flushes the console and forces any buffered output to be written
 390      * immediately .
 391      */
 392     public void flush() {
 393         pw.flush();
 394     }
 395 
 396     /**
 397      * Obtains the width of the console window.
 398      *
 399      * @return The width or -1 if it cannot be obtained.
 400      */
 401     public native int width();
 402 
 403     /**
 404      * Obtains the height of the console window.
 405      *
 406      * @return The height or -1 if it cannot be obtained.
 407      */
 408     public native int height();
 409 
 410     private Object readLock;
 411     private Object writeLock;
 412     private Reader reader;
 413     private Writer out;
 414     private PrintWriter pw;
 415     private Formatter formatter;
 416     private Charset cs;
 417     private char[] rcb;
 418     private boolean restoreEcho;
 419     private boolean shutdownHookInstalled;
 420     private static native String encoding();
 421 
 422     /*
 423      * Sets the console echo status to {@code on} and returns the previous
 424      * console on/off status.
 425      * @param on    the echo status to set to. {@code true} for echo on and
 426      *              {@code false} for echo off
 427      * @return true if the previous console echo status is on
 428      */
 429     private static native boolean echo(boolean on) throws IOException;
 430 
 431     private char[] readline(boolean zeroOut) throws IOException {
 432         int len = reader.read(rcb, 0, rcb.length);
 433         if (len < 0)
 434             return null;  //EOL
 435         if (rcb[len-1] == '\r')
 436             len--;        //remove CR at end;
 437         else if (rcb[len-1] == '\n') {
 438             len--;        //remove LF at end;
 439             if (len > 0 && rcb[len-1] == '\r')
 440                 len--;    //remove the CR, if there is one
 441         }


< prev index next >