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