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