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