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