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@1258: import java.util.Objects; jaroslav@1258: import java.util.Formatter; jaroslav@1258: import java.util.Locale; jaroslav@1258: import java.nio.charset.Charset; jaroslav@1258: import java.nio.charset.IllegalCharsetNameException; jaroslav@1258: import java.nio.charset.UnsupportedCharsetException; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints formatted representations of objects to a text-output stream. This jaroslav@1258: * class implements all of the print methods found in {@link jaroslav@1258: * PrintStream}. It does not contain methods for writing raw bytes, for which jaroslav@1258: * a program should use unencoded byte streams. jaroslav@1258: * jaroslav@1258: *

Unlike the {@link PrintStream} class, if automatic flushing is enabled jaroslav@1258: * it will be done only when one of the println, printf, or jaroslav@1258: * format methods is invoked, rather than whenever a newline character jaroslav@1258: * happens to be output. These methods use the platform's own notion of line jaroslav@1258: * separator rather than the newline character. jaroslav@1258: * jaroslav@1258: *

Methods in this class never throw I/O exceptions, although some of its jaroslav@1258: * constructors may. The client may inquire as to whether any errors have jaroslav@1258: * occurred by invoking {@link #checkError checkError()}. jaroslav@1258: * jaroslav@1258: * @author Frank Yellin jaroslav@1258: * @author Mark Reinhold jaroslav@1258: * @since JDK1.1 jaroslav@1258: */ jaroslav@1258: jaroslav@1258: public class PrintWriter extends Writer { jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * The underlying character-output stream of this jaroslav@1258: * PrintWriter. jaroslav@1258: * jaroslav@1258: * @since 1.2 jaroslav@1258: */ jaroslav@1258: protected Writer out; jaroslav@1258: jaroslav@1258: private final boolean autoFlush; jaroslav@1258: private boolean trouble = false; jaroslav@1258: private Formatter formatter; jaroslav@1258: private PrintStream psOut = null; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Line separator string. This is the value of the line.separator jaroslav@1258: * property at the moment that the stream was created. jaroslav@1258: */ jaroslav@1258: private final String lineSeparator; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns a charset object for the given charset name. jaroslav@1258: * @throws NullPointerException is csn is null jaroslav@1258: * @throws UnsupportedEncodingException if the charset is not supported jaroslav@1258: */ jaroslav@1258: private static Charset toCharset(String csn) jaroslav@1258: throws UnsupportedEncodingException jaroslav@1258: { jaroslav@1258: Objects.requireNonNull(csn, "charsetName"); jaroslav@1258: try { jaroslav@1258: return Charset.forName(csn); jaroslav@1258: } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { jaroslav@1258: // UnsupportedEncodingException should be thrown jaroslav@1258: throw new UnsupportedEncodingException(csn); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Creates a new PrintWriter, without automatic line flushing. jaroslav@1258: * jaroslav@1258: * @param out A character-output stream jaroslav@1258: */ jaroslav@1258: public PrintWriter (Writer out) { jaroslav@1258: this(out, false); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Creates a new PrintWriter. jaroslav@1258: * jaroslav@1258: * @param out A character-output stream jaroslav@1258: * @param autoFlush A boolean; if true, the println, jaroslav@1258: * printf, or format methods will jaroslav@1258: * flush the output buffer jaroslav@1258: */ jaroslav@1258: public PrintWriter(Writer out, jaroslav@1258: boolean autoFlush) { jaroslav@1258: super(out); jaroslav@1258: this.out = out; jaroslav@1258: this.autoFlush = autoFlush; jaroslav@1258: lineSeparator = java.security.AccessController.doPrivileged( jaroslav@1258: new sun.security.action.GetPropertyAction("line.separator")); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Creates a new PrintWriter, without automatic line flushing, from an jaroslav@1258: * existing OutputStream. This convenience constructor creates the jaroslav@1258: * necessary intermediate OutputStreamWriter, which will convert characters jaroslav@1258: * into bytes using the default character encoding. jaroslav@1258: * jaroslav@1258: * @param out An output stream jaroslav@1258: * jaroslav@1258: * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) jaroslav@1258: */ jaroslav@1258: public PrintWriter(OutputStream out) { jaroslav@1258: this(out, false); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Creates a new PrintWriter from an existing OutputStream. This jaroslav@1258: * convenience constructor creates the necessary intermediate jaroslav@1258: * OutputStreamWriter, which will convert characters into bytes using the jaroslav@1258: * default character encoding. jaroslav@1258: * jaroslav@1258: * @param out An output stream jaroslav@1258: * @param autoFlush A boolean; if true, the println, jaroslav@1258: * printf, or format methods will jaroslav@1258: * flush the output buffer jaroslav@1258: * jaroslav@1258: * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream) jaroslav@1258: */ jaroslav@1258: public PrintWriter(OutputStream out, boolean autoFlush) { jaroslav@1258: this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush); jaroslav@1258: jaroslav@1258: // save print stream for error propagation jaroslav@1258: if (out instanceof java.io.PrintStream) { jaroslav@1258: psOut = (PrintStream) out; jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Creates a new PrintWriter, without automatic line flushing, with the jaroslav@1258: * specified file name. 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 fileName jaroslav@1258: * The name of the file to use as the destination of this writer. jaroslav@1258: * If the file exists then it will be truncated to zero size; jaroslav@1258: * otherwise, a new file will be created. The output will be jaroslav@1258: * written to the file and is buffered. jaroslav@1258: * jaroslav@1258: * @throws FileNotFoundException jaroslav@1258: * If the given string 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 PrintWriter(String fileName) throws FileNotFoundException { jaroslav@1258: this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))), jaroslav@1258: false); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /* Private constructor */ jaroslav@1258: private PrintWriter(Charset charset, File file) jaroslav@1258: throws FileNotFoundException jaroslav@1258: { jaroslav@1258: this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)), jaroslav@1258: false); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Creates a new PrintWriter, 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 writer. jaroslav@1258: * If the file exists then it will be truncated to zero size; jaroslav@1258: * otherwise, a new file will be created. The output will be jaroslav@1258: * 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 string 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 PrintWriter(String fileName, String csn) jaroslav@1258: throws FileNotFoundException, UnsupportedEncodingException jaroslav@1258: { jaroslav@1258: this(toCharset(csn), new File(fileName)); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Creates a new PrintWriter, 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 writer. If the file jaroslav@1258: * exists then it will be truncated to zero size; otherwise, a new jaroslav@1258: * file will be created. The output will be written to the file jaroslav@1258: * 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 PrintWriter(File file) throws FileNotFoundException { jaroslav@1258: this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))), jaroslav@1258: false); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Creates a new PrintWriter, without automatic line flushing, with the jaroslav@1258: * specified file and charset. This convenience constructor creates the jaroslav@1258: * 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 writer. If the file jaroslav@1258: * exists then it will be truncated to zero size; otherwise, a new jaroslav@1258: * file will be created. The output will be written to the file jaroslav@1258: * 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(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 PrintWriter(File file, String csn) jaroslav@1258: throws FileNotFoundException, UnsupportedEncodingException jaroslav@1258: { jaroslav@1258: this(toCharset(csn), file); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** Checks 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. jaroslav@1258: * @see #checkError() jaroslav@1258: */ jaroslav@1258: public void flush() { jaroslav@1258: try { jaroslav@1258: synchronized (lock) { jaroslav@1258: ensureOpen(); jaroslav@1258: out.flush(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: catch (IOException x) { jaroslav@1258: trouble = true; jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Closes the stream and releases any system resources associated jaroslav@1258: * with it. Closing a previously closed stream has no effect. jaroslav@1258: * jaroslav@1258: * @see #checkError() jaroslav@1258: */ jaroslav@1258: public void close() { jaroslav@1258: try { jaroslav@1258: synchronized (lock) { jaroslav@1258: if (out == null) jaroslav@1258: return; jaroslav@1258: out.close(); jaroslav@1258: out = null; jaroslav@1258: } jaroslav@1258: } jaroslav@1258: catch (IOException x) { jaroslav@1258: trouble = true; jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Flushes the stream if it's not closed and checks its error state. jaroslav@1258: * jaroslav@1258: * @return true if the print stream has encountered an error, jaroslav@1258: * either on the underlying output stream or during a format jaroslav@1258: * conversion. jaroslav@1258: */ jaroslav@1258: public boolean checkError() { jaroslav@1258: if (out != null) { jaroslav@1258: flush(); jaroslav@1258: } jaroslav@1258: if (out instanceof java.io.PrintWriter) { jaroslav@1258: PrintWriter pw = (PrintWriter) out; jaroslav@1258: return pw.checkError(); jaroslav@1258: } else if (psOut != null) { jaroslav@1258: return psOut.checkError(); jaroslav@1258: } jaroslav@1258: return trouble; jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Indicates that an error has occurred. jaroslav@1258: * jaroslav@1258: *

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: protected void setError() { jaroslav@1258: trouble = true; jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Clears the 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 Writer jaroslav@1258: */ jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Writes a single character. jaroslav@1258: * @param c int specifying a character to be written. jaroslav@1258: */ jaroslav@1258: public void write(int c) { jaroslav@1258: try { jaroslav@1258: synchronized (lock) { jaroslav@1258: ensureOpen(); jaroslav@1258: out.write(c); 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 A Portion of an array of characters. jaroslav@1258: * @param buf Array of characters jaroslav@1258: * @param off Offset from which to start writing characters jaroslav@1258: * @param len Number of characters to write jaroslav@1258: */ jaroslav@1258: public void write(char buf[], int off, int len) { jaroslav@1258: try { jaroslav@1258: synchronized (lock) { jaroslav@1258: ensureOpen(); jaroslav@1258: out.write(buf, off, len); 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 an array of characters. This method cannot be inherited from the jaroslav@1258: * Writer class because it must suppress I/O exceptions. jaroslav@1258: * @param buf Array of characters to be written jaroslav@1258: */ jaroslav@1258: public void write(char buf[]) { jaroslav@1258: write(buf, 0, buf.length); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Writes a portion of a string. jaroslav@1258: * @param s A String jaroslav@1258: * @param off Offset from which to start writing characters jaroslav@1258: * @param len Number of characters to write jaroslav@1258: */ jaroslav@1258: public void write(String s, int off, int len) { jaroslav@1258: try { jaroslav@1258: synchronized (lock) { jaroslav@1258: ensureOpen(); jaroslav@1258: out.write(s, off, len); 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 a string. This method cannot be inherited from the Writer class jaroslav@1258: * because it must suppress I/O exceptions. jaroslav@1258: * @param s String to be written jaroslav@1258: */ jaroslav@1258: public void write(String s) { jaroslav@1258: write(s, 0, s.length()); jaroslav@1258: } jaroslav@1258: jaroslav@1258: private void newLine() { jaroslav@1258: try { jaroslav@1258: synchronized (lock) { jaroslav@1258: ensureOpen(); jaroslav@1258: out.write(lineSeparator); 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 {@link jaroslav@1258: * #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 {@link jaroslav@1258: * #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(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 according jaroslav@1258: * to the platform's default character encoding, and these bytes are jaroslav@1258: * written in exactly the manner of the {@link #write(int)} jaroslav@1258: * 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 {@link #write(int)} jaroslav@1258: * 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 {@link #write(int)} jaroslav@1258: * 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 {@link #write(int)} jaroslav@1258: * 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 {@link #write(int)} jaroslav@1258: * 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: /* 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 value and then terminates the line. This method behaves jaroslav@1258: * as though it invokes {@link #print(boolean)} and then jaroslav@1258: * {@link #println()}. jaroslav@1258: * jaroslav@1258: * @param x the boolean value to be printed jaroslav@1258: */ jaroslav@1258: public void println(boolean x) { jaroslav@1258: synchronized (lock) { jaroslav@1258: print(x); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints a character and then terminates the line. This method behaves as jaroslav@1258: * though it invokes {@link #print(char)} and then {@link jaroslav@1258: * #println()}. jaroslav@1258: * jaroslav@1258: * @param x the char value to be printed jaroslav@1258: */ jaroslav@1258: public void println(char x) { jaroslav@1258: synchronized (lock) { jaroslav@1258: print(x); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints an integer and then terminates the line. This method behaves as jaroslav@1258: * though it invokes {@link #print(int)} and then {@link jaroslav@1258: * #println()}. jaroslav@1258: * jaroslav@1258: * @param x the int value to be printed jaroslav@1258: */ jaroslav@1258: public void println(int x) { jaroslav@1258: synchronized (lock) { jaroslav@1258: print(x); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints a long integer and then terminates the line. This method behaves jaroslav@1258: * as though it invokes {@link #print(long)} and then jaroslav@1258: * {@link #println()}. jaroslav@1258: * jaroslav@1258: * @param x the long value to be printed jaroslav@1258: */ jaroslav@1258: public void println(long x) { jaroslav@1258: synchronized (lock) { jaroslav@1258: print(x); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints a floating-point number and then terminates the line. This method jaroslav@1258: * behaves as though it invokes {@link #print(float)} and then jaroslav@1258: * {@link #println()}. jaroslav@1258: * jaroslav@1258: * @param x the float value to be printed jaroslav@1258: */ jaroslav@1258: public void println(float x) { jaroslav@1258: synchronized (lock) { jaroslav@1258: print(x); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints a double-precision floating-point number and then terminates the jaroslav@1258: * line. This method behaves as though it invokes {@link jaroslav@1258: * #print(double)} and then {@link #println()}. jaroslav@1258: * jaroslav@1258: * @param x the double value to be printed jaroslav@1258: */ jaroslav@1258: public void println(double x) { jaroslav@1258: synchronized (lock) { jaroslav@1258: print(x); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints an array of characters and then terminates the line. This method jaroslav@1258: * behaves as though it invokes {@link #print(char[])} and then jaroslav@1258: * {@link #println()}. jaroslav@1258: * jaroslav@1258: * @param x the array of char values to be printed jaroslav@1258: */ jaroslav@1258: public void println(char x[]) { jaroslav@1258: synchronized (lock) { jaroslav@1258: print(x); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints a String and then terminates 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 value to be printed jaroslav@1258: */ jaroslav@1258: public void println(String x) { jaroslav@1258: synchronized (lock) { jaroslav@1258: print(x); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Prints an Object and then terminates 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 (lock) { jaroslav@1258: print(s); jaroslav@1258: println(); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * A convenience method to write a formatted string to this writer using jaroslav@1258: * the specified format string and arguments. If automatic flushing is jaroslav@1258: * enabled, calls to this method will flush the output buffer. jaroslav@1258: * jaroslav@1258: *

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: *

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 writer jaroslav@1258: * jaroslav@1258: * @since 1.5 jaroslav@1258: */ jaroslav@1258: public PrintWriter printf(String format, Object ... args) { jaroslav@1258: return format(format, args); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * A convenience method to write a formatted string to this writer using jaroslav@1258: * the specified format string and arguments. If automatic flushing is jaroslav@1258: * enabled, calls to this method will flush the output buffer. 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: *

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 writer jaroslav@1258: * jaroslav@1258: * @since 1.5 jaroslav@1258: */ jaroslav@1258: public PrintWriter printf(Locale l, String format, Object ... args) { jaroslav@1258: return format(l, format, args); jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Writes a formatted string to this writer using the specified format jaroslav@1258: * string and arguments. If automatic flushing is enabled, calls to this jaroslav@1258: * method will flush the output buffer. 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 writer jaroslav@1258: * jaroslav@1258: * @since 1.5 jaroslav@1258: */ jaroslav@1258: public PrintWriter format(String format, Object ... args) { jaroslav@1258: try { jaroslav@1258: synchronized (lock) { jaroslav@1258: ensureOpen(); jaroslav@1258: if ((formatter == null) jaroslav@1258: || (formatter.locale() != Locale.getDefault())) jaroslav@1258: formatter = new Formatter(this); jaroslav@1258: formatter.format(Locale.getDefault(), format, args); jaroslav@1258: if (autoFlush) jaroslav@1258: out.flush(); jaroslav@1258: } jaroslav@1258: } catch (InterruptedIOException x) { jaroslav@1258: Thread.currentThread().interrupt(); jaroslav@1258: } catch (IOException x) { jaroslav@1258: trouble = true; jaroslav@1258: } jaroslav@1258: return this; jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Writes a formatted string to this writer using the specified format jaroslav@1258: * string and arguments. If automatic flushing is enabled, calls to this jaroslav@1258: * method will flush the output buffer. 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 writer jaroslav@1258: * jaroslav@1258: * @since 1.5 jaroslav@1258: */ jaroslav@1258: public PrintWriter format(Locale l, String format, Object ... args) { jaroslav@1258: try { jaroslav@1258: synchronized (lock) { jaroslav@1258: ensureOpen(); jaroslav@1258: if ((formatter == null) || (formatter.locale() != l)) jaroslav@1258: formatter = new Formatter(this, l); jaroslav@1258: formatter.format(l, format, args); jaroslav@1258: if (autoFlush) jaroslav@1258: out.flush(); jaroslav@1258: } jaroslav@1258: } catch (InterruptedIOException x) { jaroslav@1258: Thread.currentThread().interrupt(); jaroslav@1258: } catch (IOException x) { jaroslav@1258: trouble = true; jaroslav@1258: } jaroslav@1258: return this; jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Appends the specified character sequence to this writer. 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: *

jaroslav@1258:      *     out.write(csq.toString()) 
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 the 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 writer. jaroslav@1258: * jaroslav@1258: * @return This writer jaroslav@1258: * jaroslav@1258: * @since 1.5 jaroslav@1258: */ jaroslav@1258: public PrintWriter append(CharSequence csq) { jaroslav@1258: if (csq == null) jaroslav@1258: write("null"); jaroslav@1258: else jaroslav@1258: write(csq.toString()); jaroslav@1258: return this; jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Appends a subsequence of the specified character sequence to this writer. 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: *

jaroslav@1258:      *     out.write(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 writer 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 PrintWriter 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 writer. 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: *

jaroslav@1258:      *     out.write(c) 
jaroslav@1258: * jaroslav@1258: * @param c jaroslav@1258: * The 16-bit character to append jaroslav@1258: * jaroslav@1258: * @return This writer jaroslav@1258: * jaroslav@1258: * @since 1.5 jaroslav@1258: */ jaroslav@1258: public PrintWriter append(char c) { jaroslav@1258: write(c); jaroslav@1258: return this; jaroslav@1258: } jaroslav@1258: }