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