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