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