0N/A/*
3377N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/Aimport java.util.Formatter;
0N/Aimport java.util.Locale;
3377N/Aimport java.nio.charset.Charset;
3377N/Aimport java.nio.charset.IllegalCharsetNameException;
3377N/Aimport java.nio.charset.UnsupportedCharsetException;
0N/A
0N/A/**
0N/A * A <code>PrintStream</code> adds functionality to another output stream,
0N/A * namely the ability to print representations of various data values
0N/A * conveniently. Two other features are provided as well. Unlike other output
0N/A * streams, a <code>PrintStream</code> never throws an
0N/A * <code>IOException</code>; instead, exceptional situations merely set an
0N/A * internal flag that can be tested via the <code>checkError</code> method.
0N/A * Optionally, a <code>PrintStream</code> can be created so as to flush
0N/A * automatically; this means that the <code>flush</code> method is
0N/A * automatically invoked after a byte array is written, one of the
0N/A * <code>println</code> methods is invoked, or a newline character or byte
0N/A * (<code>'\n'</code>) is written.
0N/A *
0N/A * <p> All characters printed by a <code>PrintStream</code> are converted into
0N/A * bytes using the platform's default character encoding. The <code>{@link
0N/A * PrintWriter}</code> class should be used in situations that require writing
0N/A * characters rather than bytes.
0N/A *
0N/A * @author Frank Yellin
0N/A * @author Mark Reinhold
0N/A * @since JDK1.0
0N/A */
0N/A
0N/Apublic class PrintStream extends FilterOutputStream
0N/A implements Appendable, Closeable
0N/A{
0N/A
3377N/A private final boolean autoFlush;
0N/A private boolean trouble = false;
0N/A private Formatter formatter;
0N/A
0N/A /**
0N/A * Track both the text- and character-output streams, so that their buffers
0N/A * can be flushed without flushing the entire stream.
0N/A */
0N/A private BufferedWriter textOut;
0N/A private OutputStreamWriter charOut;
0N/A
0N/A /**
3479N/A * requireNonNull is explicitly declared here so as not to create an extra
3479N/A * dependency on java.util.Objects.requireNonNull. PrintStream is loaded
3377N/A * early during system initialization.
3377N/A */
3479N/A private static <T> T requireNonNull(T obj, String message) {
3377N/A if (obj == null)
3377N/A throw new NullPointerException(message);
3377N/A return obj;
3377N/A }
3377N/A
3377N/A /**
3377N/A * Returns a charset object for the given charset name.
3377N/A * @throws NullPointerException is csn is null
3377N/A * @throws UnsupportedEncodingException if the charset is not supported
3377N/A */
3377N/A private static Charset toCharset(String csn)
3377N/A throws UnsupportedEncodingException
3377N/A {
3479N/A requireNonNull(csn, "charsetName");
3377N/A try {
3377N/A return Charset.forName(csn);
3377N/A } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
3377N/A // UnsupportedEncodingException should be thrown
3377N/A throw new UnsupportedEncodingException(csn);
3377N/A }
3377N/A }
3377N/A
3377N/A /* Private constructors */
3377N/A private PrintStream(boolean autoFlush, OutputStream out) {
3377N/A super(out);
3377N/A this.autoFlush = autoFlush;
3377N/A this.charOut = new OutputStreamWriter(this);
3377N/A this.textOut = new BufferedWriter(charOut);
3377N/A }
3377N/A
3377N/A private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {
3377N/A super(out);
3377N/A this.autoFlush = autoFlush;
3377N/A this.charOut = new OutputStreamWriter(this, charset);
3377N/A this.textOut = new BufferedWriter(charOut);
3377N/A }
3377N/A
3377N/A /* Variant of the private constructor so that the given charset name
3377N/A * can be verified before evaluating the OutputStream argument. Used
3377N/A * by constructors creating a FileOutputStream that also take a
3377N/A * charset name.
3377N/A */
3377N/A private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
3377N/A throws UnsupportedEncodingException
3377N/A {
3377N/A this(autoFlush, out, charset);
3377N/A }
3377N/A
3377N/A /**
0N/A * Creates a new print stream. This stream will not flush automatically.
0N/A *
0N/A * @param out The output stream to which values and objects will be
0N/A * printed
0N/A *
0N/A * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
0N/A */
0N/A public PrintStream(OutputStream out) {
0N/A this(out, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new print stream.
0N/A *
0N/A * @param out The output stream to which values and objects will be
0N/A * printed
0N/A * @param autoFlush A boolean; if true, the output buffer will be flushed
0N/A * whenever a byte array is written, one of the
0N/A * <code>println</code> methods is invoked, or a newline
0N/A * character or byte (<code>'\n'</code>) is written
0N/A *
0N/A * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
0N/A */
0N/A public PrintStream(OutputStream out, boolean autoFlush) {
3479N/A this(autoFlush, requireNonNull(out, "Null output stream"));
0N/A }
0N/A
0N/A /**
0N/A * Creates a new print stream.
0N/A *
0N/A * @param out The output stream to which values and objects will be
0N/A * printed
0N/A * @param autoFlush A boolean; if true, the output buffer will be flushed
0N/A * whenever a byte array is written, one of the
0N/A * <code>println</code> methods is invoked, or a newline
0N/A * character or byte (<code>'\n'</code>) is written
0N/A * @param encoding The name of a supported
0N/A * <a href="../lang/package-summary.html#charenc">
0N/A * character encoding</a>
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named encoding is not supported
0N/A *
0N/A * @since 1.4
0N/A */
0N/A public PrintStream(OutputStream out, boolean autoFlush, String encoding)
0N/A throws UnsupportedEncodingException
0N/A {
3377N/A this(autoFlush,
3479N/A requireNonNull(out, "Null output stream"),
3377N/A toCharset(encoding));
0N/A }
0N/A
0N/A /**
0N/A * Creates a new print stream, without automatic line flushing, with the
0N/A * specified file name. This convenience constructor creates
0N/A * the necessary intermediate {@link java.io.OutputStreamWriter
0N/A * OutputStreamWriter}, which will encode characters using the
0N/A * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
0N/A * for this instance of the Java virtual machine.
0N/A *
0N/A * @param fileName
0N/A * The name of the file to use as the destination of this print
0N/A * stream. If the file exists, then it will be truncated to
0N/A * zero size; otherwise, a new file will be created. The output
0N/A * will be written to the file and is buffered.
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file object does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(fileName)} denies write
0N/A * access to the file
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream(String fileName) throws FileNotFoundException {
0N/A this(false, new FileOutputStream(fileName));
0N/A }
0N/A
0N/A /**
0N/A * Creates a new print stream, without automatic line flushing, with the
0N/A * specified file name and charset. This convenience constructor creates
0N/A * the necessary intermediate {@link java.io.OutputStreamWriter
0N/A * OutputStreamWriter}, which will encode characters using the provided
0N/A * charset.
0N/A *
0N/A * @param fileName
0N/A * The name of the file to use as the destination of this print
0N/A * stream. If the file exists, then it will be truncated to
0N/A * zero size; otherwise, a new file will be created. The output
0N/A * will be written to the file and is buffered.
0N/A *
0N/A * @param csn
0N/A * The name of a supported {@linkplain java.nio.charset.Charset
0N/A * charset}
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file object does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(fileName)} denies write
0N/A * access to the file
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream(String fileName, String csn)
0N/A throws FileNotFoundException, UnsupportedEncodingException
0N/A {
3377N/A // ensure charset is checked before the file is opened
3377N/A this(false, toCharset(csn), new FileOutputStream(fileName));
0N/A }
0N/A
0N/A /**
0N/A * Creates a new print stream, without automatic line flushing, with the
0N/A * specified file. This convenience constructor creates the necessary
0N/A * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
0N/A * which will encode characters using the {@linkplain
0N/A * java.nio.charset.Charset#defaultCharset() default charset} for this
0N/A * instance of the Java virtual machine.
0N/A *
0N/A * @param file
0N/A * The file to use as the destination of this print stream. If the
0N/A * file exists, then it will be truncated to zero size; otherwise,
0N/A * a new file will be created. The output will be written to the
0N/A * file and is buffered.
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file object does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is present and {@link
0N/A * SecurityManager#checkWrite checkWrite(file.getPath())}
0N/A * denies write access to the file
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream(File file) throws FileNotFoundException {
0N/A this(false, new FileOutputStream(file));
0N/A }
0N/A
0N/A /**
0N/A * Creates a new print stream, without automatic line flushing, with the
0N/A * specified file and charset. This convenience constructor creates
0N/A * the necessary intermediate {@link java.io.OutputStreamWriter
0N/A * OutputStreamWriter}, which will encode characters using the provided
0N/A * charset.
0N/A *
0N/A * @param file
0N/A * The file to use as the destination of this print stream. If the
0N/A * file exists, then it will be truncated to zero size; otherwise,
0N/A * a new file will be created. The output will be written to the
0N/A * file and is buffered.
0N/A *
0N/A * @param csn
0N/A * The name of a supported {@linkplain java.nio.charset.Charset
0N/A * charset}
0N/A *
0N/A * @throws FileNotFoundException
0N/A * If the given file object does not denote an existing, writable
0N/A * regular file and a new regular file of that name cannot be
0N/A * created, or if some other error occurs while opening or
0N/A * creating the file
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager is presentand {@link
0N/A * SecurityManager#checkWrite checkWrite(file.getPath())}
0N/A * denies write access to the file
0N/A *
0N/A * @throws UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream(File file, String csn)
0N/A throws FileNotFoundException, UnsupportedEncodingException
0N/A {
3377N/A // ensure charset is checked before the file is opened
3377N/A this(false, toCharset(csn), new FileOutputStream(file));
0N/A }
0N/A
0N/A /** Check to make sure that the stream has not been closed */
0N/A private void ensureOpen() throws IOException {
0N/A if (out == null)
0N/A throw new IOException("Stream closed");
0N/A }
0N/A
0N/A /**
0N/A * Flushes the stream. This is done by writing any buffered output bytes to
0N/A * the underlying output stream and then flushing that stream.
0N/A *
0N/A * @see java.io.OutputStream#flush()
0N/A */
0N/A public void flush() {
0N/A synchronized (this) {
0N/A try {
0N/A ensureOpen();
0N/A out.flush();
0N/A }
0N/A catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A private boolean closing = false; /* To avoid recursive closing */
0N/A
0N/A /**
0N/A * Closes the stream. This is done by flushing the stream and then closing
0N/A * the underlying output stream.
0N/A *
0N/A * @see java.io.OutputStream#close()
0N/A */
0N/A public void close() {
0N/A synchronized (this) {
0N/A if (! closing) {
0N/A closing = true;
0N/A try {
0N/A textOut.close();
0N/A out.close();
0N/A }
0N/A catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A textOut = null;
0N/A charOut = null;
0N/A out = null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Flushes the stream and checks its error state. The internal error state
0N/A * is set to <code>true</code> when the underlying output stream throws an
0N/A * <code>IOException</code> other than <code>InterruptedIOException</code>,
0N/A * and when the <code>setError</code> method is invoked. If an operation
0N/A * on the underlying output stream throws an
0N/A * <code>InterruptedIOException</code>, then the <code>PrintStream</code>
0N/A * converts the exception back into an interrupt by doing:
0N/A * <pre>
0N/A * Thread.currentThread().interrupt();
0N/A * </pre>
0N/A * or the equivalent.
0N/A *
0N/A * @return <code>true</code> if and only if this stream has encountered an
0N/A * <code>IOException</code> other than
0N/A * <code>InterruptedIOException</code>, or the
0N/A * <code>setError</code> method has been invoked
0N/A */
0N/A public boolean checkError() {
0N/A if (out != null)
0N/A flush();
0N/A if (out instanceof java.io.PrintStream) {
0N/A PrintStream ps = (PrintStream) out;
0N/A return ps.checkError();
0N/A }
0N/A return trouble;
0N/A }
0N/A
0N/A /**
0N/A * Sets the error state of the stream to <code>true</code>.
0N/A *
0N/A * <p> This method will cause subsequent invocations of {@link
0N/A * #checkError()} to return <tt>true</tt> until {@link
0N/A * #clearError()} is invoked.
0N/A *
0N/A * @since JDK1.1
0N/A */
0N/A protected void setError() {
0N/A trouble = true;
0N/A }
0N/A
0N/A /**
0N/A * Clears the internal error state of this stream.
0N/A *
0N/A * <p> This method will cause subsequent invocations of {@link
0N/A * #checkError()} to return <tt>false</tt> until another write
0N/A * operation fails and invokes {@link #setError()}.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A protected void clearError() {
0N/A trouble = false;
0N/A }
0N/A
0N/A /*
0N/A * Exception-catching, synchronized output operations,
0N/A * which also implement the write() methods of OutputStream
0N/A */
0N/A
0N/A /**
0N/A * Writes the specified byte to this stream. If the byte is a newline and
0N/A * automatic flushing is enabled then the <code>flush</code> method will be
0N/A * invoked.
0N/A *
0N/A * <p> Note that the byte is written as given; to write a character that
0N/A * will be translated according to the platform's default character
0N/A * encoding, use the <code>print(char)</code> or <code>println(char)</code>
0N/A * methods.
0N/A *
0N/A * @param b The byte to be written
0N/A * @see #print(char)
0N/A * @see #println(char)
0N/A */
0N/A public void write(int b) {
0N/A try {
0N/A synchronized (this) {
0N/A ensureOpen();
0N/A out.write(b);
0N/A if ((b == '\n') && autoFlush)
0N/A out.flush();
0N/A }
0N/A }
0N/A catch (InterruptedIOException x) {
0N/A Thread.currentThread().interrupt();
0N/A }
0N/A catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Writes <code>len</code> bytes from the specified byte array starting at
0N/A * offset <code>off</code> to this stream. If automatic flushing is
0N/A * enabled then the <code>flush</code> method will be invoked.
0N/A *
0N/A * <p> Note that the bytes will be written as given; to write characters
0N/A * that will be translated according to the platform's default character
0N/A * encoding, use the <code>print(char)</code> or <code>println(char)</code>
0N/A * methods.
0N/A *
0N/A * @param buf A byte array
0N/A * @param off Offset from which to start taking bytes
0N/A * @param len Number of bytes to write
0N/A */
0N/A public void write(byte buf[], int off, int len) {
0N/A try {
0N/A synchronized (this) {
0N/A ensureOpen();
0N/A out.write(buf, off, len);
0N/A if (autoFlush)
0N/A out.flush();
0N/A }
0N/A }
0N/A catch (InterruptedIOException x) {
0N/A Thread.currentThread().interrupt();
0N/A }
0N/A catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The following private methods on the text- and character-output streams
0N/A * always flush the stream buffers, so that writes to the underlying byte
0N/A * stream occur as promptly as with the original PrintStream.
0N/A */
0N/A
0N/A private void write(char buf[]) {
0N/A try {
0N/A synchronized (this) {
0N/A ensureOpen();
0N/A textOut.write(buf);
0N/A textOut.flushBuffer();
0N/A charOut.flushBuffer();
0N/A if (autoFlush) {
0N/A for (int i = 0; i < buf.length; i++)
0N/A if (buf[i] == '\n')
0N/A out.flush();
0N/A }
0N/A }
0N/A }
0N/A catch (InterruptedIOException x) {
0N/A Thread.currentThread().interrupt();
0N/A }
0N/A catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A }
0N/A
0N/A private void write(String s) {
0N/A try {
0N/A synchronized (this) {
0N/A ensureOpen();
0N/A textOut.write(s);
0N/A textOut.flushBuffer();
0N/A charOut.flushBuffer();
0N/A if (autoFlush && (s.indexOf('\n') >= 0))
0N/A out.flush();
0N/A }
0N/A }
0N/A catch (InterruptedIOException x) {
0N/A Thread.currentThread().interrupt();
0N/A }
0N/A catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A }
0N/A
0N/A private void newLine() {
0N/A try {
0N/A synchronized (this) {
0N/A ensureOpen();
0N/A textOut.newLine();
0N/A textOut.flushBuffer();
0N/A charOut.flushBuffer();
0N/A if (autoFlush)
0N/A out.flush();
0N/A }
0N/A }
0N/A catch (InterruptedIOException x) {
0N/A Thread.currentThread().interrupt();
0N/A }
0N/A catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A }
0N/A
0N/A /* Methods that do not terminate lines */
0N/A
0N/A /**
0N/A * Prints a boolean value. The string produced by <code>{@link
0N/A * java.lang.String#valueOf(boolean)}</code> is translated into bytes
0N/A * according to the platform's default character encoding, and these bytes
0N/A * are written in exactly the manner of the
0N/A * <code>{@link #write(int)}</code> method.
0N/A *
0N/A * @param b The <code>boolean</code> to be printed
0N/A */
0N/A public void print(boolean b) {
0N/A write(b ? "true" : "false");
0N/A }
0N/A
0N/A /**
0N/A * Prints a character. The character is translated into one or more bytes
0N/A * according to the platform's default character encoding, and these bytes
0N/A * are written in exactly the manner of the
0N/A * <code>{@link #write(int)}</code> method.
0N/A *
0N/A * @param c The <code>char</code> to be printed
0N/A */
0N/A public void print(char c) {
0N/A write(String.valueOf(c));
0N/A }
0N/A
0N/A /**
0N/A * Prints an integer. The string produced by <code>{@link
0N/A * java.lang.String#valueOf(int)}</code> is translated into bytes
0N/A * according to the platform's default character encoding, and these bytes
0N/A * are written in exactly the manner of the
0N/A * <code>{@link #write(int)}</code> method.
0N/A *
0N/A * @param i The <code>int</code> to be printed
0N/A * @see java.lang.Integer#toString(int)
0N/A */
0N/A public void print(int i) {
0N/A write(String.valueOf(i));
0N/A }
0N/A
0N/A /**
0N/A * Prints a long integer. The string produced by <code>{@link
0N/A * java.lang.String#valueOf(long)}</code> is translated into bytes
0N/A * according to the platform's default character encoding, and these bytes
0N/A * are written in exactly the manner of the
0N/A * <code>{@link #write(int)}</code> method.
0N/A *
0N/A * @param l The <code>long</code> to be printed
0N/A * @see java.lang.Long#toString(long)
0N/A */
0N/A public void print(long l) {
0N/A write(String.valueOf(l));
0N/A }
0N/A
0N/A /**
0N/A * Prints a floating-point number. The string produced by <code>{@link
0N/A * java.lang.String#valueOf(float)}</code> is translated into bytes
0N/A * according to the platform's default character encoding, and these bytes
0N/A * are written in exactly the manner of the
0N/A * <code>{@link #write(int)}</code> method.
0N/A *
0N/A * @param f The <code>float</code> to be printed
0N/A * @see java.lang.Float#toString(float)
0N/A */
0N/A public void print(float f) {
0N/A write(String.valueOf(f));
0N/A }
0N/A
0N/A /**
0N/A * Prints a double-precision floating-point number. The string produced by
0N/A * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
0N/A * bytes according to the platform's default character encoding, and these
0N/A * bytes are written in exactly the manner of the <code>{@link
0N/A * #write(int)}</code> method.
0N/A *
0N/A * @param d The <code>double</code> to be printed
0N/A * @see java.lang.Double#toString(double)
0N/A */
0N/A public void print(double d) {
0N/A write(String.valueOf(d));
0N/A }
0N/A
0N/A /**
0N/A * Prints an array of characters. The characters are converted into bytes
0N/A * according to the platform's default character encoding, and these bytes
0N/A * are written in exactly the manner of the
0N/A * <code>{@link #write(int)}</code> method.
0N/A *
0N/A * @param s The array of chars to be printed
0N/A *
0N/A * @throws NullPointerException If <code>s</code> is <code>null</code>
0N/A */
0N/A public void print(char s[]) {
0N/A write(s);
0N/A }
0N/A
0N/A /**
0N/A * Prints a string. If the argument is <code>null</code> then the string
0N/A * <code>"null"</code> is printed. Otherwise, the string's characters are
0N/A * converted into bytes according to the platform's default character
0N/A * encoding, and these bytes are written in exactly the manner of the
0N/A * <code>{@link #write(int)}</code> method.
0N/A *
0N/A * @param s The <code>String</code> to be printed
0N/A */
0N/A public void print(String s) {
0N/A if (s == null) {
0N/A s = "null";
0N/A }
0N/A write(s);
0N/A }
0N/A
0N/A /**
0N/A * Prints an object. The string produced by the <code>{@link
0N/A * java.lang.String#valueOf(Object)}</code> method is translated into bytes
0N/A * according to the platform's default character encoding, and these bytes
0N/A * are written in exactly the manner of the
0N/A * <code>{@link #write(int)}</code> method.
0N/A *
0N/A * @param obj The <code>Object</code> to be printed
0N/A * @see java.lang.Object#toString()
0N/A */
0N/A public void print(Object obj) {
0N/A write(String.valueOf(obj));
0N/A }
0N/A
0N/A
0N/A /* Methods that do terminate lines */
0N/A
0N/A /**
0N/A * Terminates the current line by writing the line separator string. The
0N/A * line separator string is defined by the system property
0N/A * <code>line.separator</code>, and is not necessarily a single newline
0N/A * character (<code>'\n'</code>).
0N/A */
0N/A public void println() {
0N/A newLine();
0N/A }
0N/A
0N/A /**
0N/A * Prints a boolean and then terminate the line. This method behaves as
0N/A * though it invokes <code>{@link #print(boolean)}</code> and then
0N/A * <code>{@link #println()}</code>.
0N/A *
0N/A * @param x The <code>boolean</code> to be printed
0N/A */
0N/A public void println(boolean x) {
0N/A synchronized (this) {
0N/A print(x);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints a character and then terminate the line. This method behaves as
0N/A * though it invokes <code>{@link #print(char)}</code> and then
0N/A * <code>{@link #println()}</code>.
0N/A *
0N/A * @param x The <code>char</code> to be printed.
0N/A */
0N/A public void println(char x) {
0N/A synchronized (this) {
0N/A print(x);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints an integer and then terminate the line. This method behaves as
0N/A * though it invokes <code>{@link #print(int)}</code> and then
0N/A * <code>{@link #println()}</code>.
0N/A *
0N/A * @param x The <code>int</code> to be printed.
0N/A */
0N/A public void println(int x) {
0N/A synchronized (this) {
0N/A print(x);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints a long and then terminate the line. This method behaves as
0N/A * though it invokes <code>{@link #print(long)}</code> and then
0N/A * <code>{@link #println()}</code>.
0N/A *
0N/A * @param x a The <code>long</code> to be printed.
0N/A */
0N/A public void println(long x) {
0N/A synchronized (this) {
0N/A print(x);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints a float and then terminate the line. This method behaves as
0N/A * though it invokes <code>{@link #print(float)}</code> and then
0N/A * <code>{@link #println()}</code>.
0N/A *
0N/A * @param x The <code>float</code> to be printed.
0N/A */
0N/A public void println(float x) {
0N/A synchronized (this) {
0N/A print(x);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints a double and then terminate the line. This method behaves as
0N/A * though it invokes <code>{@link #print(double)}</code> and then
0N/A * <code>{@link #println()}</code>.
0N/A *
0N/A * @param x The <code>double</code> to be printed.
0N/A */
0N/A public void println(double x) {
0N/A synchronized (this) {
0N/A print(x);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints an array of characters and then terminate the line. This method
0N/A * behaves as though it invokes <code>{@link #print(char[])}</code> and
0N/A * then <code>{@link #println()}</code>.
0N/A *
0N/A * @param x an array of chars to print.
0N/A */
0N/A public void println(char x[]) {
0N/A synchronized (this) {
0N/A print(x);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints a String and then terminate the line. This method behaves as
0N/A * though it invokes <code>{@link #print(String)}</code> and then
0N/A * <code>{@link #println()}</code>.
0N/A *
0N/A * @param x The <code>String</code> to be printed.
0N/A */
0N/A public void println(String x) {
0N/A synchronized (this) {
0N/A print(x);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints an Object and then terminate the line. This method calls
0N/A * at first String.valueOf(x) to get the printed object's string value,
0N/A * then behaves as
0N/A * though it invokes <code>{@link #print(String)}</code> and then
0N/A * <code>{@link #println()}</code>.
0N/A *
0N/A * @param x The <code>Object</code> to be printed.
0N/A */
0N/A public void println(Object x) {
0N/A String s = String.valueOf(x);
0N/A synchronized (this) {
0N/A print(s);
0N/A newLine();
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * A convenience method to write a formatted string to this output stream
0N/A * using the specified format string and arguments.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>out.printf(format,
0N/A * args)</tt> behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * out.format(format, args) </pre>
0N/A *
0N/A * @param format
0N/A * A format string as described in <a
0N/A * href="../util/Formatter.html#syntax">Format string syntax</a>
0N/A *
0N/A * @param args
0N/A * Arguments referenced by the format specifiers in the format
0N/A * string. If there are more arguments than format specifiers, the
0N/A * extra arguments are ignored. The number of arguments is
0N/A * variable and may be zero. The maximum number of arguments is
0N/A * limited by the maximum dimension of a Java array as defined by
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>.
4008N/A * The behaviour on a
0N/A * <tt>null</tt> argument depends on the <a
0N/A * href="../util/Formatter.html#syntax">conversion</a>.
0N/A *
0N/A * @throws IllegalFormatException
0N/A * If a format string contains an illegal syntax, a format
0N/A * specifier that is incompatible with the given arguments,
0N/A * insufficient arguments given the format string, or other
0N/A * illegal conditions. For specification of all possible
0N/A * formatting errors, see the <a
0N/A * href="../util/Formatter.html#detail">Details</a> section of the
0N/A * formatter class specification.
0N/A *
0N/A * @throws NullPointerException
0N/A * If the <tt>format</tt> is <tt>null</tt>
0N/A *
0N/A * @return This output stream
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream printf(String format, Object ... args) {
0N/A return format(format, args);
0N/A }
0N/A
0N/A /**
0N/A * A convenience method to write a formatted string to this output stream
0N/A * using the specified format string and arguments.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>out.printf(l, format,
0N/A * args)</tt> behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * out.format(l, format, args) </pre>
0N/A *
0N/A * @param l
0N/A * The {@linkplain java.util.Locale locale} to apply during
0N/A * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
0N/A * is applied.
0N/A *
0N/A * @param format
0N/A * A format string as described in <a
0N/A * href="../util/Formatter.html#syntax">Format string syntax</a>
0N/A *
0N/A * @param args
0N/A * Arguments referenced by the format specifiers in the format
0N/A * string. If there are more arguments than format specifiers, the
0N/A * extra arguments are ignored. The number of arguments is
0N/A * variable and may be zero. The maximum number of arguments is
0N/A * limited by the maximum dimension of a Java array as defined by
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>.
4008N/A * The behaviour on a
0N/A * <tt>null</tt> argument depends on the <a
0N/A * href="../util/Formatter.html#syntax">conversion</a>.
0N/A *
0N/A * @throws IllegalFormatException
0N/A * If a format string contains an illegal syntax, a format
0N/A * specifier that is incompatible with the given arguments,
0N/A * insufficient arguments given the format string, or other
0N/A * illegal conditions. For specification of all possible
0N/A * formatting errors, see the <a
0N/A * href="../util/Formatter.html#detail">Details</a> section of the
0N/A * formatter class specification.
0N/A *
0N/A * @throws NullPointerException
0N/A * If the <tt>format</tt> is <tt>null</tt>
0N/A *
0N/A * @return This output stream
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream printf(Locale l, String format, Object ... args) {
0N/A return format(l, format, args);
0N/A }
0N/A
0N/A /**
0N/A * Writes a formatted string to this output stream using the specified
0N/A * format string and arguments.
0N/A *
0N/A * <p> The locale always used is the one returned by {@link
0N/A * java.util.Locale#getDefault() Locale.getDefault()}, regardless of any
0N/A * previous invocations of other formatting methods on this object.
0N/A *
0N/A * @param format
0N/A * A format string as described in <a
0N/A * href="../util/Formatter.html#syntax">Format string syntax</a>
0N/A *
0N/A * @param args
0N/A * Arguments referenced by the format specifiers in the format
0N/A * string. If there are more arguments than format specifiers, the
0N/A * extra arguments are ignored. The number of arguments is
0N/A * variable and may be zero. The maximum number of arguments is
0N/A * limited by the maximum dimension of a Java array as defined by
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>.
4008N/A * The behaviour on a
0N/A * <tt>null</tt> argument depends on the <a
0N/A * href="../util/Formatter.html#syntax">conversion</a>.
0N/A *
0N/A * @throws IllegalFormatException
0N/A * If a format string contains an illegal syntax, a format
0N/A * specifier that is incompatible with the given arguments,
0N/A * insufficient arguments given the format string, or other
0N/A * illegal conditions. For specification of all possible
0N/A * formatting errors, see the <a
0N/A * href="../util/Formatter.html#detail">Details</a> section of the
0N/A * formatter class specification.
0N/A *
0N/A * @throws NullPointerException
0N/A * If the <tt>format</tt> is <tt>null</tt>
0N/A *
0N/A * @return This output stream
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream format(String format, Object ... args) {
0N/A try {
0N/A synchronized (this) {
0N/A ensureOpen();
0N/A if ((formatter == null)
0N/A || (formatter.locale() != Locale.getDefault()))
0N/A formatter = new Formatter((Appendable) this);
0N/A formatter.format(Locale.getDefault(), format, args);
0N/A }
0N/A } catch (InterruptedIOException x) {
0N/A Thread.currentThread().interrupt();
0N/A } catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Writes a formatted string to this output stream using the specified
0N/A * format string and arguments.
0N/A *
0N/A * @param l
0N/A * The {@linkplain java.util.Locale locale} to apply during
0N/A * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
0N/A * is applied.
0N/A *
0N/A * @param format
0N/A * A format string as described in <a
0N/A * href="../util/Formatter.html#syntax">Format string syntax</a>
0N/A *
0N/A * @param args
0N/A * Arguments referenced by the format specifiers in the format
0N/A * string. If there are more arguments than format specifiers, the
0N/A * extra arguments are ignored. The number of arguments is
0N/A * variable and may be zero. The maximum number of arguments is
0N/A * limited by the maximum dimension of a Java array as defined by
4008N/A * <cite>The Java&trade; Virtual Machine Specification</cite>.
4008N/A * The behaviour on a
0N/A * <tt>null</tt> argument depends on the <a
0N/A * href="../util/Formatter.html#syntax">conversion</a>.
0N/A *
0N/A * @throws IllegalFormatException
0N/A * If a format string contains an illegal syntax, a format
0N/A * specifier that is incompatible with the given arguments,
0N/A * insufficient arguments given the format string, or other
0N/A * illegal conditions. For specification of all possible
0N/A * formatting errors, see the <a
0N/A * href="../util/Formatter.html#detail">Details</a> section of the
0N/A * formatter class specification.
0N/A *
0N/A * @throws NullPointerException
0N/A * If the <tt>format</tt> is <tt>null</tt>
0N/A *
0N/A * @return This output stream
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream format(Locale l, String format, Object ... args) {
0N/A try {
0N/A synchronized (this) {
0N/A ensureOpen();
0N/A if ((formatter == null)
0N/A || (formatter.locale() != l))
0N/A formatter = new Formatter(this, l);
0N/A formatter.format(l, format, args);
0N/A }
0N/A } catch (InterruptedIOException x) {
0N/A Thread.currentThread().interrupt();
0N/A } catch (IOException x) {
0N/A trouble = true;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified character sequence to this output stream.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
0N/A * behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * out.print(csq.toString()) </pre>
0N/A *
0N/A * <p> Depending on the specification of <tt>toString</tt> for the
0N/A * character sequence <tt>csq</tt>, the entire sequence may not be
0N/A * appended. For instance, invoking then <tt>toString</tt> method of a
0N/A * character buffer will return a subsequence whose content depends upon
0N/A * the buffer's position and limit.
0N/A *
0N/A * @param csq
0N/A * The character sequence to append. If <tt>csq</tt> is
0N/A * <tt>null</tt>, then the four characters <tt>"null"</tt> are
0N/A * appended to this output stream.
0N/A *
0N/A * @return This output stream
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream append(CharSequence csq) {
0N/A if (csq == null)
0N/A print("null");
0N/A else
0N/A print(csq.toString());
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Appends a subsequence of the specified character sequence to this output
0N/A * stream.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>out.append(csq, start,
0N/A * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
0N/A * exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * out.print(csq.subSequence(start, end).toString()) </pre>
0N/A *
0N/A * @param csq
0N/A * The character sequence from which a subsequence will be
0N/A * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
0N/A * will be appended as if <tt>csq</tt> contained the four
0N/A * characters <tt>"null"</tt>.
0N/A *
0N/A * @param start
0N/A * The index of the first character in the subsequence
0N/A *
0N/A * @param end
0N/A * The index of the character following the last character in the
0N/A * subsequence
0N/A *
0N/A * @return This output stream
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
0N/A * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
0N/A * <tt>csq.length()</tt>
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream append(CharSequence csq, int start, int end) {
0N/A CharSequence cs = (csq == null ? "null" : csq);
0N/A write(cs.subSequence(start, end).toString());
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified character to this output stream.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>out.append(c)</tt>
0N/A * behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * out.print(c) </pre>
0N/A *
0N/A * @param c
0N/A * The 16-bit character to append
0N/A *
0N/A * @return This output stream
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public PrintStream append(char c) {
0N/A print(c);
0N/A return this;
0N/A }
0N/A
0N/A}