src/java.base/share/classes/java/io/Console.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 49924 84d0fe3cefd4
child 53018 8bf9268df0e2
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49924
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2703
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.charset.Charset;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49924
diff changeset
    30
import jdk.internal.access.JavaIOAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49924
diff changeset
    31
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.nio.cs.StreamDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.nio.cs.StreamEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Methods to access the character-based console device, if any, associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * with the current Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p> Whether a virtual machine has a console is dependent upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * underlying platform and also upon the manner in which the virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * machine is invoked.  If the virtual machine is started from an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * interactive command line without redirecting the standard input and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * output streams then its console will exist and will typically be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * connected to the keyboard and display from which the virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * was launched.  If the virtual machine is started automatically, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * example by a background job scheduler, then it will typically not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * have a console.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * If this virtual machine has a console then it is represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * unique instance of this class which can be obtained by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@link java.lang.System#console()} method.  If no console device is
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
    52
 * available then an invocation of that method will return {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Read and write operations are synchronized to guarantee the atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * completion of critical operations; therefore invoking methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link #readLine()}, {@link #readPassword()}, {@link #format format()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link #printf printf()} as well as the read, format and write operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * on the objects returned by {@link #reader()} and {@link #writer()} may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * block in multithreaded scenarios.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
    61
 * Invoking {@code close()} on the objects returned by the {@link #reader()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * and the {@link #writer()} will not close the underlying stream of those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
    65
 * The console-read methods return {@code null} when the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * console input stream is reached, for example by typing control-D on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Unix or control-Z on Windows.  Subsequent read operations will succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * if additional characters are later entered on the console's input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
    71
 * Unless otherwise specified, passing a {@code null} argument to any method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * in this class will cause a {@link NullPointerException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <b>Security note:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * If an application needs to read a password or other secure data, it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * use {@link #readPassword()} or {@link #readPassword(String, Object...)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * manually zero the returned character array after processing to minimize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * lifetime of sensitive data in memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 9275
diff changeset
    80
 * <blockquote><pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Console cons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * char[] passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * if ((cons = System.console()) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     (passwd = cons.readPassword("[%s]", "Password:")) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     java.util.Arrays.fill(passwd, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * }
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 9275
diff changeset
    88
 * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @author  Xueming Shen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
public final class Console implements Flushable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    * Retrieves the unique {@link java.io.PrintWriter PrintWriter} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    * associated with this console.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    * @return  The printwriter associated with this console
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public PrintWriter writer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    * Retrieves the unique {@link java.io.Reader Reader} object associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    * with this console.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    * This method is intended to be used by sophisticated applications, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    * example, a {@link java.util.Scanner} object which utilizes the rich
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   112
    * parsing/scanning functionality provided by the {@code Scanner}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    * Console con = System.console();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    * if (con != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    *     Scanner sc = new Scanner(con.reader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    *     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    * For simple applications requiring only line-oriented reading, use
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   122
    * {@link #readLine}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    * The bulk read operations {@link java.io.Reader#read(char[]) read(char[]) },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    * {@link java.io.Reader#read(char[], int, int) read(char[], int, int) } and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    * {@link java.io.Reader#read(java.nio.CharBuffer) read(java.nio.CharBuffer)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    * on the returned object will not read in characters beyond the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    * bound for each invocation, even if the destination buffer has space for
19810
98b738773420 6341345: (spec) Console.reader() should make it clear that the reader requires line termination
sherman
parents: 18156
diff changeset
   129
    * more characters. The {@code Reader}'s {@code read} methods may block if a
98b738773420 6341345: (spec) Console.reader() should make it clear that the reader requires line termination
sherman
parents: 18156
diff changeset
   130
    * line bound has not been entered or reached on the console's input device.
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   131
    * A line bound is considered to be any one of a line feed ({@code '\n'}),
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   132
    * a carriage return ({@code '\r'}), a carriage return followed immediately
19810
98b738773420 6341345: (spec) Console.reader() should make it clear that the reader requires line termination
sherman
parents: 18156
diff changeset
   133
    * by a linefeed, or an end of stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    * @return  The reader associated with this console
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public Reader reader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    * Writes a formatted string to this console's output stream using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    * the specified format string and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    * @param  fmt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    *         A format string as described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    *         href="../util/Formatter.html#syntax">Format string syntax</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    *         extra arguments are ignored.  The number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    *         variable and may be zero.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    *         limited by the maximum dimension of a Java array as defined by
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8389
diff changeset
   155
    *         <cite>The Java&trade; Virtual Machine Specification</cite>.
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8389
diff changeset
   156
    *         The behaviour on a
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   157
    *         {@code null} argument depends on the <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    *         href="../util/Formatter.html#syntax">conversion</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    *          href="../util/Formatter.html#detail">Details</a> section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    *          of the formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    * @return  This console
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public Console format(String fmt, Object ...args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        formatter.format(fmt, args).flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    * A convenience method to write a formatted string to this console's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    * output stream using the specified format string and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   180
    * <p> An invocation of this method of the form
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   181
    * {@code con.printf(format, args)} behaves in exactly the same way
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   182
    * as the invocation of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    * <pre>con.format(format, args)</pre>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    * @param  format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    *         A format string as described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    *         href="../util/Formatter.html#syntax">Format string syntax</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    *         extra arguments are ignored.  The number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    *         variable and may be zero.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    *         limited by the maximum dimension of a Java array as defined by
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8389
diff changeset
   195
    *         <cite>The Java&trade; Virtual Machine Specification</cite>.
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8389
diff changeset
   196
    *         The behaviour on a
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   197
    *         {@code null} argument depends on the <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    *         href="../util/Formatter.html#syntax">conversion</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    *          href="../util/Formatter.html#detail">Details</a> section of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    *          formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    * @return  This console
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public Console printf(String format, Object ... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return format(format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    * Provides a formatted prompt, then reads a single line of text from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    * console.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    * @param  fmt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    *         A format string as described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    *         href="../util/Formatter.html#syntax">Format string syntax</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    *         extra arguments are ignored.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    *         limited by the maximum dimension of a Java array as defined by
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8389
diff changeset
   228
    *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    *          href="../util/Formatter.html#detail">Details</a> section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    *          of the formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    * @throws IOError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    *         If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    * @return  A string containing the line read from the console, not
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   243
    *          including any line-termination characters, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    *          if an end of stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public String readLine(String fmt, Object ... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        String line = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            synchronized(readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (fmt.length() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    pw.format(fmt, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    char[] ca = readline(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    if (ca != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        line = new String(ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    throw new IOError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    * Reads a single line of text from the console.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    * @throws IOError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    *         If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    * @return  A string containing the line read from the console, not
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   271
    *          including any line-termination characters, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    *          if an end of stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public String readLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return readLine("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    * Provides a formatted prompt, then reads a password or passphrase from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    * the console with echoing disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    * @param  fmt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    *         A format string as described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    *         href="../util/Formatter.html#syntax">Format string syntax</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    *         for the prompt text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    *         extra arguments are ignored.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    *         limited by the maximum dimension of a Java array as defined by
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8389
diff changeset
   292
    *         <cite>The Java&trade; Virtual Machine Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    *          href="../util/Formatter.html#detail">Details</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    *          section of the formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    * @throws IOError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    *         If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    * @return  A character array containing the password or passphrase read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    *          from the console, not including any line-termination characters,
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   308
    *          or {@code null} if an end of stream has been reached.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public char[] readPassword(String fmt, Object ... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        char[] passwd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            synchronized(readLock) {
49924
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   314
                installShutdownHook();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                try {
49924
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   316
                    restoreEcho = echo(false);
8389
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   317
                } catch (IOException x) {
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   318
                    throw new IOError(x);
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   319
                }
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   320
                IOError ioe = null;
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   321
                try {
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   322
                    if (fmt.length() != 0)
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   323
                        pw.format(fmt, args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    passwd = readline(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                } catch (IOException x) {
8389
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   326
                    ioe = new IOError(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    try {
49924
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   329
                        if (restoreEcho)
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   330
                            restoreEcho = echo(true);
8389
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   331
                    } catch (IOException x) {
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   332
                        if (ioe == null)
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   333
                            ioe = new IOError(x);
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   334
                        else
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   335
                            ioe.addSuppressed(x);
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   336
                    }
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   337
                    if (ioe != null)
3d0196408a7a 6996192: Console.readPassword race: input echo off must be prior to writing prompt
sherman
parents: 5506
diff changeset
   338
                        throw ioe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
49924
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   346
    private void installShutdownHook() {
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   347
        if (shutdownHookInstalled)
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   348
            return;
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   349
        try {
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   350
            // Add a shutdown hook to restore console's echo state should
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   351
            // it be necessary.
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   352
            SharedSecrets.getJavaLangAccess()
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   353
                .registerShutdownHook(0 /* shutdown hook invocation order */,
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   354
                    false /* only register if shutdown is not in progress */,
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   355
                    new Runnable() {
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   356
                        public void run() {
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   357
                            try {
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   358
                                if (restoreEcho) {
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   359
                                    echo(true);
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   360
                                }
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   361
                            } catch (IOException x) { }
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   362
                        }
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   363
                    });
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   364
        } catch (IllegalStateException e) {
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   365
            // shutdown is already in progress and readPassword is first used
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   366
            // by a shutdown hook
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   367
        }
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   368
        shutdownHookInstalled = true;
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   369
    }
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   370
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    * Reads a password or passphrase from the console with echoing disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    * @throws IOError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    *         If an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    * @return  A character array containing the password or passphrase read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    *          from the console, not including any line-termination characters,
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   379
    *          or {@code null} if an end of stream has been reached.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public char[] readPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return readPassword("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Flushes the console and forces any buffered output to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * immediately .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        pw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    private Object readLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private Object writeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private Reader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private Writer out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    private PrintWriter pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    private Formatter formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private Charset cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private char[] rcb;
49924
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   401
    private boolean restoreEcho;
84d0fe3cefd4 8202105: Console echo is disabled when exiting jshell
jlahoda
parents: 49764
diff changeset
   402
    private boolean shutdownHookInstalled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private static native String encoding();
49764
906712e6afbf 8194750: Console.readPassword does not save/restore tty settings
sherman
parents: 47216
diff changeset
   404
    /*
906712e6afbf 8194750: Console.readPassword does not save/restore tty settings
sherman
parents: 47216
diff changeset
   405
     * Sets the console echo status to {@code on} and returns the previous
906712e6afbf 8194750: Console.readPassword does not save/restore tty settings
sherman
parents: 47216
diff changeset
   406
     * console on/off status.
906712e6afbf 8194750: Console.readPassword does not save/restore tty settings
sherman
parents: 47216
diff changeset
   407
     * @param on    the echo status to set to. {@code true} for echo on and
906712e6afbf 8194750: Console.readPassword does not save/restore tty settings
sherman
parents: 47216
diff changeset
   408
     *              {@code false} for echo off
906712e6afbf 8194750: Console.readPassword does not save/restore tty settings
sherman
parents: 47216
diff changeset
   409
     * @return true if the previous console echo status is on
906712e6afbf 8194750: Console.readPassword does not save/restore tty settings
sherman
parents: 47216
diff changeset
   410
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private static native boolean echo(boolean on) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private char[] readline(boolean zeroOut) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        int len = reader.read(rcb, 0, rcb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (len < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return null;  //EOL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (rcb[len-1] == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            len--;        //remove CR at end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        else if (rcb[len-1] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            len--;        //remove LF at end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (len > 0 && rcb[len-1] == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                len--;    //remove the CR, if there is one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        char[] b = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            System.arraycopy(rcb, 0, b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (zeroOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                Arrays.fill(rcb, 0, len, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    private char[] grow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        assert Thread.holdsLock(readLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        char[] t = new char[rcb.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        System.arraycopy(rcb, 0, t, 0, rcb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        rcb = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return rcb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    class LineReader extends Reader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        private Reader in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        private char[] cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        private int nChars, nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        boolean leftoverLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        LineReader(Reader in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            cb = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            nextChar = nChars = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            leftoverLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        public void close () {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            //in.ready synchronizes on readLock already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return in.ready();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        public int read(char cbuf[], int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            int off = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            int end = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (offset < 0 || offset > cbuf.length || length < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                end < 0 || end > cbuf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            synchronized(readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                boolean eof = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                char c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    if (nextChar >= nChars) {   //fill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                            n = in.read(cb, 0, cb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        } while (n == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                            nChars = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                            nextChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                            if (n < cb.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                cb[n-1] != '\n' && cb[n-1] != '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                 * we're in canonical mode so each "fill" should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                 * come back with an eol. if there no lf or nl at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                 * the end of returned bytes we reached an eof.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        } else { /*EOF*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                            if (off - offset == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                            return off - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    if (leftoverLF && cbuf == rcb && cb[nextChar] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                         * if invoked by our readline, skip the leftover, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                         * return the LF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    leftoverLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    while (nextChar < nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        c = cbuf[off++] = cb[nextChar];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        cb[nextChar++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            return off - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        } else if (c == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                            if (off == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                /* no space left even the next is LF, so return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                 * whatever we have if the invoker is not our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                 * readLine()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                if (cbuf == rcb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                    cbuf = grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                    end = cbuf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                    leftoverLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                    return off - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                            if (nextChar == nChars && in.ready()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                 * we have a CR and we reached the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                 * the read in buffer, fill to make sure we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                 * don't miss a LF, if there is one, it's possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                 * that it got cut off during last round reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                 * simply because the read in buffer was full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                nChars = in.read(cb, 0, cb.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                nextChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                            if (nextChar < nChars && cb[nextChar] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                cbuf[off++] = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                            return off - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        } else if (off == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                           if (cbuf == rcb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                cbuf = grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                end = cbuf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                           } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                               return off - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    if (eof)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                        return off - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    // Set up JavaIOAccess in SharedSecrets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    static {
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
   556
        SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            public Console console() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                if (istty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    if (cons == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        cons = new Console();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    return cons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            public Charset charset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                // This method is called in sun.security.util.Password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                // cons already exists when this method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                return cons.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    private static Console cons;
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
   574
    private static native boolean istty();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    private Console() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        readLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        writeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        String csname = encoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (csname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                cs = Charset.forName(csname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            } catch (Exception x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (cs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            cs = Charset.defaultCharset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        out = StreamEncoder.forOutputStreamWriter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                  new FileOutputStream(FileDescriptor.out),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                  writeLock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                  cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        pw = new PrintWriter(out, true) { public void close() {} };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        formatter = new Formatter(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        reader = new LineReader(StreamDecoder.forInputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                     new FileInputStream(FileDescriptor.in),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                     readLock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                     cs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        rcb = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
}