jdk/src/share/classes/java/io/PrintStream.java
author mchung
Thu, 12 Mar 2009 10:27:22 -0700
changeset 2277 445a331b4a8b
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6810254: Lazily instantiate the shared secret access objects Summary: Register the shutdown hooks only when needed and remove JavaIODeleteOnExitAccess Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.Formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A <code>PrintStream</code> adds functionality to another output stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * namely the ability to print representations of various data values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * conveniently.  Two other features are provided as well.  Unlike other output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * streams, a <code>PrintStream</code> never throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>IOException</code>; instead, exceptional situations merely set an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * internal flag that can be tested via the <code>checkError</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Optionally, a <code>PrintStream</code> can be created so as to flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * automatically; this means that the <code>flush</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * automatically invoked after a byte array is written, one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>println</code> methods is invoked, or a newline character or byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * (<code>'\n'</code>) is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> All characters printed by a <code>PrintStream</code> are converted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * bytes using the platform's default character encoding.  The <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * PrintWriter}</code> class should be used in situations that require writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * characters rather than bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author     Frank Yellin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author     Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class PrintStream extends FilterOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    implements Appendable, Closeable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean autoFlush = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean trouble = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private Formatter formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Track both the text- and character-output streams, so that their buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * can be flushed without flushing the entire stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private BufferedWriter textOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private OutputStreamWriter charOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Creates a new print stream.  This stream will not flush automatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param  out        The output stream to which values and objects will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *                    printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public PrintStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this(out, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /* Initialization is factored into a private constructor (note the swapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * parameters so that this one isn't confused with the public one) and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * separate init method so that the following two public constructors can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * share code.  We use a separate init method so that the constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * takes an encoding will throw an NPE for a null stream before it throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * an UnsupportedEncodingException for an unsupported encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private PrintStream(boolean autoFlush, OutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (out == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new NullPointerException("Null output stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.autoFlush = autoFlush;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private void init(OutputStreamWriter osw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.charOut = osw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.textOut = new BufferedWriter(osw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Creates a new print stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param  out        The output stream to which values and objects will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *                    printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param  autoFlush  A boolean; if true, the output buffer will be flushed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *                    whenever a byte array is written, one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *                    <code>println</code> methods is invoked, or a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *                    character or byte (<code>'\n'</code>) is written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public PrintStream(OutputStream out, boolean autoFlush) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this(autoFlush, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        init(new OutputStreamWriter(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Creates a new print stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param  out        The output stream to which values and objects will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *                    printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param  autoFlush  A boolean; if true, the output buffer will be flushed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *                    whenever a byte array is written, one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *                    <code>println</code> methods is invoked, or a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *                    character or byte (<code>'\n'</code>) is written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param  encoding   The name of a supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *                    <a href="../lang/package-summary.html#charenc">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *                    character encoding</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws  UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *          If the named encoding is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public PrintStream(OutputStream out, boolean autoFlush, String encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this(autoFlush, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        init(new OutputStreamWriter(this, encoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Creates a new print stream, without automatic line flushing, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * specified file name.  This convenience constructor creates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * the necessary intermediate {@link java.io.OutputStreamWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * OutputStreamWriter}, which will encode characters using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * for this instance of the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param  fileName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         The name of the file to use as the destination of this print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         stream.  If the file exists, then it will be truncated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         zero size; otherwise, a new file will be created.  The output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *         will be written to the file and is buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws  FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *          If the given file object does not denote an existing, writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *          regular file and a new regular file of that name cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *          created, or if some other error occurs while opening or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *          creating the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *          If a security manager is present and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *          SecurityManager#checkWrite checkWrite(fileName)} denies write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *          access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public PrintStream(String fileName) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this(false, new FileOutputStream(fileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        init(new OutputStreamWriter(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Creates a new print stream, without automatic line flushing, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * specified file name and charset.  This convenience constructor creates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * the necessary intermediate {@link java.io.OutputStreamWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * OutputStreamWriter}, which will encode characters using the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param  fileName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *         The name of the file to use as the destination of this print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *         stream.  If the file exists, then it will be truncated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         zero size; otherwise, a new file will be created.  The output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *         will be written to the file and is buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param  csn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *         The name of a supported {@linkplain java.nio.charset.Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *         charset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @throws  FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *          If the given file object does not denote an existing, writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *          regular file and a new regular file of that name cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *          created, or if some other error occurs while opening or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *          creating the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *          If a security manager is present and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *          SecurityManager#checkWrite checkWrite(fileName)} denies write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @throws  UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *          If the named charset is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public PrintStream(String fileName, String csn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        throws FileNotFoundException, UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this(false, new FileOutputStream(fileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        init(new OutputStreamWriter(this, csn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Creates a new print stream, without automatic line flushing, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * specified file.  This convenience constructor creates the necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * which will encode characters using the {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * java.nio.charset.Charset#defaultCharset() default charset} for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * instance of the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param  file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *         The file to use as the destination of this print stream.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *         file exists, then it will be truncated to zero size; otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *         a new file will be created.  The output will be written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *         file and is buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @throws  FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *          If the given file object does not denote an existing, writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *          regular file and a new regular file of that name cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *          created, or if some other error occurs while opening or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *          creating the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *          If a security manager is present and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *          SecurityManager#checkWrite checkWrite(file.getPath())}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *          denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public PrintStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        this(false, new FileOutputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        init(new OutputStreamWriter(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Creates a new print stream, without automatic line flushing, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * specified file and charset.  This convenience constructor creates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * the necessary intermediate {@link java.io.OutputStreamWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * OutputStreamWriter}, which will encode characters using the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param  file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *         The file to use as the destination of this print stream.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         file exists, then it will be truncated to zero size; otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *         a new file will be created.  The output will be written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *         file and is buffered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param  csn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *         The name of a supported {@linkplain java.nio.charset.Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *         charset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @throws  FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          If the given file object does not denote an existing, writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          regular file and a new regular file of that name cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          created, or if some other error occurs while opening or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          creating the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          If a security manager is presentand {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *          SecurityManager#checkWrite checkWrite(file.getPath())}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @throws  UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *          If the named charset is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public PrintStream(File file, String csn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        throws FileNotFoundException, UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        this(false, new FileOutputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        init(new OutputStreamWriter(this, csn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /** Check to make sure that the stream has not been closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (out == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Flushes the stream.  This is done by writing any buffered output bytes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * the underlying output stream and then flushing that stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see        java.io.OutputStream#flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private boolean closing = false; /* To avoid recursive closing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Closes the stream.  This is done by flushing the stream and then closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * the underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @see        java.io.OutputStream#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (! closing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                closing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    textOut.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                textOut = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                charOut = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Flushes the stream and checks its error state. The internal error state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * is set to <code>true</code> when the underlying output stream throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>IOException</code> other than <code>InterruptedIOException</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * and when the <code>setError</code> method is invoked.  If an operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * on the underlying output stream throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <code>InterruptedIOException</code>, then the <code>PrintStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * converts the exception back into an interrupt by doing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *     Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * or the equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return <code>true</code> if and only if this stream has encountered an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *         <code>IOException</code> other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *         <code>InterruptedIOException</code>, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *         <code>setError</code> method has been invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public boolean checkError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (out != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (out instanceof java.io.PrintStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            PrintStream ps = (PrintStream) out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return ps.checkError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return trouble;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Sets the error state of the stream to <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <p> This method will cause subsequent invocations of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * #checkError()} to return <tt>true</tt> until {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * #clearError()} is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    protected void setError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Clears the internal error state of this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <p> This method will cause subsequent invocations of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * #checkError()} to return <tt>false</tt> until another write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * operation fails and invokes {@link #setError()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    protected void clearError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        trouble = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Exception-catching, synchronized output operations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * which also implement the write() methods of OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Writes the specified byte to this stream.  If the byte is a newline and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * automatic flushing is enabled then the <code>flush</code> method will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p> Note that the byte is written as given; to write a character that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * will be translated according to the platform's default character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * encoding, use the <code>print(char)</code> or <code>println(char)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param  b  The byte to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see #print(char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see #println(char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public void write(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                if ((b == '\n') && autoFlush)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        catch (InterruptedIOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Writes <code>len</code> bytes from the specified byte array starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * offset <code>off</code> to this stream.  If automatic flushing is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * enabled then the <code>flush</code> method will be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <p> Note that the bytes will be written as given; to write characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * that will be translated according to the platform's default character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * encoding, use the <code>print(char)</code> or <code>println(char)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param  buf   A byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param  off   Offset from which to start taking bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param  len   Number of bytes to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public void write(byte buf[], int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                out.write(buf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                if (autoFlush)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        catch (InterruptedIOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * The following private methods on the text- and character-output streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * always flush the stream buffers, so that writes to the underlying byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * stream occur as promptly as with the original PrintStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private void write(char buf[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                textOut.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                textOut.flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                charOut.flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                if (autoFlush) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    for (int i = 0; i < buf.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        if (buf[i] == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        catch (InterruptedIOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    private void write(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                textOut.write(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                textOut.flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                charOut.flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                if (autoFlush && (s.indexOf('\n') >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        catch (InterruptedIOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private void newLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                textOut.newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                textOut.flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                charOut.flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                if (autoFlush)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        catch (InterruptedIOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /* Methods that do not terminate lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Prints a boolean value.  The string produced by <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * java.lang.String#valueOf(boolean)}</code> is translated into bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * according to the platform's default character encoding, and these bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * are written in exactly the manner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <code>{@link #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @param      b   The <code>boolean</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public void print(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        write(b ? "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Prints a character.  The character is translated into one or more bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * according to the platform's default character encoding, and these bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * are written in exactly the manner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <code>{@link #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param      c   The <code>char</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public void print(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        write(String.valueOf(c));
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
     * Prints an integer.  The string produced by <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * java.lang.String#valueOf(int)}</code> is translated into bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * according to the platform's default character encoding, and these bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * are written in exactly the manner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <code>{@link #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param      i   The <code>int</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @see        java.lang.Integer#toString(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public void print(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        write(String.valueOf(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Prints a long integer.  The string produced by <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * java.lang.String#valueOf(long)}</code> is translated into bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * according to the platform's default character encoding, and these bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * are written in exactly the manner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <code>{@link #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param      l   The <code>long</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @see        java.lang.Long#toString(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public void print(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        write(String.valueOf(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Prints a floating-point number.  The string produced by <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * java.lang.String#valueOf(float)}</code> is translated into bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * according to the platform's default character encoding, and these bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * are written in exactly the manner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <code>{@link #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param      f   The <code>float</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @see        java.lang.Float#toString(float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public void print(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        write(String.valueOf(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Prints a double-precision floating-point number.  The string produced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * bytes according to the platform's default character encoding, and these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * bytes are written in exactly the manner of the <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @param      d   The <code>double</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @see        java.lang.Double#toString(double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public void print(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        write(String.valueOf(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Prints an array of characters.  The characters are converted into bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * according to the platform's default character encoding, and these bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * are written in exactly the manner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * <code>{@link #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @param      s   The array of chars to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @throws  NullPointerException  If <code>s</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public void print(char s[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        write(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Prints a string.  If the argument is <code>null</code> then the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <code>"null"</code> is printed.  Otherwise, the string's characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * converted into bytes according to the platform's default character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * encoding, and these bytes are written in exactly the manner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <code>{@link #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param      s   The <code>String</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public void print(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            s = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        write(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Prints an object.  The string produced by the <code>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * java.lang.String#valueOf(Object)}</code> method is translated into bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * according to the platform's default character encoding, and these bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * are written in exactly the manner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * <code>{@link #write(int)}</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param      obj   The <code>Object</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @see        java.lang.Object#toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    public void print(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        write(String.valueOf(obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /* Methods that do terminate lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Terminates the current line by writing the line separator string.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * line separator string is defined by the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <code>line.separator</code>, and is not necessarily a single newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * character (<code>'\n'</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public void println() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * Prints a boolean and then terminate the line.  This method behaves as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * though it invokes <code>{@link #print(boolean)}</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param x  The <code>boolean</code> to be printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    public void println(boolean x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            print(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Prints a character and then terminate the line.  This method behaves as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * though it invokes <code>{@link #print(char)}</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @param x  The <code>char</code> to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public void println(char x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            print(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Prints an integer and then terminate the line.  This method behaves as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * though it invokes <code>{@link #print(int)}</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @param x  The <code>int</code> to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public void println(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            print(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Prints a long and then terminate the line.  This method behaves as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * though it invokes <code>{@link #print(long)}</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @param x  a The <code>long</code> to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public void println(long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            print(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * Prints a float and then terminate the line.  This method behaves as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * though it invokes <code>{@link #print(float)}</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @param x  The <code>float</code> to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    public void println(float x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            print(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Prints a double and then terminate the line.  This method behaves as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * though it invokes <code>{@link #print(double)}</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @param x  The <code>double</code> to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    public void println(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            print(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Prints an array of characters and then terminate the line.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * behaves as though it invokes <code>{@link #print(char[])}</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * then <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @param x  an array of chars to print.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public void println(char x[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            print(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * Prints a String and then terminate the line.  This method behaves as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * though it invokes <code>{@link #print(String)}</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @param x  The <code>String</code> to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public void println(String x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            print(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Prints an Object and then terminate the line.  This method calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * at first String.valueOf(x) to get the printed object's string value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * then behaves as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * though it invokes <code>{@link #print(String)}</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * <code>{@link #println()}</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @param x  The <code>Object</code> to be printed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    public void println(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        String s = String.valueOf(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * A convenience method to write a formatted string to this output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * using the specified format string and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * <p> An invocation of this method of the form <tt>out.printf(format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * args)</tt> behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *     out.format(format, args) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param  format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *         A format string as described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *         href="../util/Formatter.html#syntax">Format string syntax</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *         extra arguments are ignored.  The number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *         variable and may be zero.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *         limited by the maximum dimension of a Java array as defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *         the <a href="http://java.sun.com/docs/books/vmspec/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *         Virtual Machine Specification</a>.  The behaviour on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *         <tt>null</tt> argument depends on the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *         href="../util/Formatter.html#syntax">conversion</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *          href="../util/Formatter.html#detail">Details</a> section of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *          formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *          If the <tt>format</tt> is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @return  This output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    public PrintStream printf(String format, Object ... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return format(format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * A convenience method to write a formatted string to this output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * using the specified format string and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * <p> An invocation of this method of the form <tt>out.printf(l, format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * args)</tt> behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *     out.format(l, format, args) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @param  l
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *         The {@linkplain java.util.Locale locale} to apply during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *         is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param  format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *         A format string as described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *         href="../util/Formatter.html#syntax">Format string syntax</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *         extra arguments are ignored.  The number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *         variable and may be zero.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *         limited by the maximum dimension of a Java array as defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *         the <a href="http://java.sun.com/docs/books/vmspec/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *         Virtual Machine Specification</a>.  The behaviour on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *         <tt>null</tt> argument depends on the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *         href="../util/Formatter.html#syntax">conversion</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *          href="../util/Formatter.html#detail">Details</a> section of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *          formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *          If the <tt>format</tt> is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @return  This output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    public PrintStream printf(Locale l, String format, Object ... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        return format(l, format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * Writes a formatted string to this output stream using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * format string and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * <p> The locale always used is the one returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * java.util.Locale#getDefault() Locale.getDefault()}, regardless of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * previous invocations of other formatting methods on this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @param  format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *         A format string as described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *         href="../util/Formatter.html#syntax">Format string syntax</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *         extra arguments are ignored.  The number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *         variable and may be zero.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     *         limited by the maximum dimension of a Java array as defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *         the <a href="http://java.sun.com/docs/books/vmspec/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *         Virtual Machine Specification</a>.  The behaviour on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *         <tt>null</tt> argument depends on the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *         href="../util/Formatter.html#syntax">conversion</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *          href="../util/Formatter.html#detail">Details</a> section of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *          formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *          If the <tt>format</tt> is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @return  This output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    public PrintStream format(String format, Object ... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                if ((formatter == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    || (formatter.locale() != Locale.getDefault()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    formatter = new Formatter((Appendable) this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                formatter.format(Locale.getDefault(), format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        } catch (InterruptedIOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * Writes a formatted string to this output stream using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * format string and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @param  l
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *         The {@linkplain java.util.Locale locale} to apply during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *         is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @param  format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *         A format string as described in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *         href="../util/Formatter.html#syntax">Format string syntax</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *         extra arguments are ignored.  The number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *         variable and may be zero.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *         limited by the maximum dimension of a Java array as defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *         the <a href="http://java.sun.com/docs/books/vmspec/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *         Virtual Machine Specification</a>.  The behaviour on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     *         <tt>null</tt> argument depends on the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     *         href="../util/Formatter.html#syntax">conversion</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @throws  IllegalFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *          href="../util/Formatter.html#detail">Details</a> section of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *          formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *          If the <tt>format</tt> is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @return  This output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    public PrintStream format(Locale l, String format, Object ... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                if ((formatter == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    || (formatter.locale() != l))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    formatter = new Formatter(this, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                formatter.format(l, format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        } catch (InterruptedIOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            trouble = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * Appends the specified character sequence to this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *     out.print(csq.toString()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * <p> Depending on the specification of <tt>toString</tt> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * character sequence <tt>csq</tt>, the entire sequence may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * appended.  For instance, invoking then <tt>toString</tt> method of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * character buffer will return a subsequence whose content depends upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * the buffer's position and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *         The character sequence to append.  If <tt>csq</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *         appended to this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @return  This output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    public PrintStream append(CharSequence csq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (csq == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            print("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            print(csq.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * Appends a subsequence of the specified character sequence to this output
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <p> An invocation of this method of the form <tt>out.append(csq, start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *     out.print(csq.subSequence(start, end).toString()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *         The character sequence from which a subsequence will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *         will be appended as if <tt>csq</tt> contained the four
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *         characters <tt>"null"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *         The index of the first character in the subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *         The index of the character following the last character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *         subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @return  This output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *          <tt>csq.length()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    public PrintStream append(CharSequence csq, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        CharSequence cs = (csq == null ? "null" : csq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        write(cs.subSequence(start, end).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * Appends the specified character to this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *     out.print(c) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *         The 16-bit character to append
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @return  This output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    public PrintStream append(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        print(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
}