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