1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/io/PrintWriter.java Sat Sep 07 13:51:24 2013 +0200
1.3 @@ -0,0 +1,1066 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.io;
1.30 +
1.31 +import java.util.Objects;
1.32 +import java.util.Formatter;
1.33 +import java.util.Locale;
1.34 +import java.nio.charset.Charset;
1.35 +import java.nio.charset.IllegalCharsetNameException;
1.36 +import java.nio.charset.UnsupportedCharsetException;
1.37 +
1.38 +/**
1.39 + * Prints formatted representations of objects to a text-output stream. This
1.40 + * class implements all of the <tt>print</tt> methods found in {@link
1.41 + * PrintStream}. It does not contain methods for writing raw bytes, for which
1.42 + * a program should use unencoded byte streams.
1.43 + *
1.44 + * <p> Unlike the {@link PrintStream} class, if automatic flushing is enabled
1.45 + * it will be done only when one of the <tt>println</tt>, <tt>printf</tt>, or
1.46 + * <tt>format</tt> methods is invoked, rather than whenever a newline character
1.47 + * happens to be output. These methods use the platform's own notion of line
1.48 + * separator rather than the newline character.
1.49 + *
1.50 + * <p> Methods in this class never throw I/O exceptions, although some of its
1.51 + * constructors may. The client may inquire as to whether any errors have
1.52 + * occurred by invoking {@link #checkError checkError()}.
1.53 + *
1.54 + * @author Frank Yellin
1.55 + * @author Mark Reinhold
1.56 + * @since JDK1.1
1.57 + */
1.58 +
1.59 +public class PrintWriter extends Writer {
1.60 +
1.61 + /**
1.62 + * The underlying character-output stream of this
1.63 + * <code>PrintWriter</code>.
1.64 + *
1.65 + * @since 1.2
1.66 + */
1.67 + protected Writer out;
1.68 +
1.69 + private final boolean autoFlush;
1.70 + private boolean trouble = false;
1.71 + private Formatter formatter;
1.72 + private PrintStream psOut = null;
1.73 +
1.74 + /**
1.75 + * Line separator string. This is the value of the line.separator
1.76 + * property at the moment that the stream was created.
1.77 + */
1.78 + private final String lineSeparator;
1.79 +
1.80 + /**
1.81 + * Returns a charset object for the given charset name.
1.82 + * @throws NullPointerException is csn is null
1.83 + * @throws UnsupportedEncodingException if the charset is not supported
1.84 + */
1.85 + private static Charset toCharset(String csn)
1.86 + throws UnsupportedEncodingException
1.87 + {
1.88 + Objects.requireNonNull(csn, "charsetName");
1.89 + try {
1.90 + return Charset.forName(csn);
1.91 + } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
1.92 + // UnsupportedEncodingException should be thrown
1.93 + throw new UnsupportedEncodingException(csn);
1.94 + }
1.95 + }
1.96 +
1.97 + /**
1.98 + * Creates a new PrintWriter, without automatic line flushing.
1.99 + *
1.100 + * @param out A character-output stream
1.101 + */
1.102 + public PrintWriter (Writer out) {
1.103 + this(out, false);
1.104 + }
1.105 +
1.106 + /**
1.107 + * Creates a new PrintWriter.
1.108 + *
1.109 + * @param out A character-output stream
1.110 + * @param autoFlush A boolean; if true, the <tt>println</tt>,
1.111 + * <tt>printf</tt>, or <tt>format</tt> methods will
1.112 + * flush the output buffer
1.113 + */
1.114 + public PrintWriter(Writer out,
1.115 + boolean autoFlush) {
1.116 + super(out);
1.117 + this.out = out;
1.118 + this.autoFlush = autoFlush;
1.119 + lineSeparator = java.security.AccessController.doPrivileged(
1.120 + new sun.security.action.GetPropertyAction("line.separator"));
1.121 + }
1.122 +
1.123 + /**
1.124 + * Creates a new PrintWriter, without automatic line flushing, from an
1.125 + * existing OutputStream. This convenience constructor creates the
1.126 + * necessary intermediate OutputStreamWriter, which will convert characters
1.127 + * into bytes using the default character encoding.
1.128 + *
1.129 + * @param out An output stream
1.130 + *
1.131 + * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
1.132 + */
1.133 + public PrintWriter(OutputStream out) {
1.134 + this(out, false);
1.135 + }
1.136 +
1.137 + /**
1.138 + * Creates a new PrintWriter from an existing OutputStream. This
1.139 + * convenience constructor creates the necessary intermediate
1.140 + * OutputStreamWriter, which will convert characters into bytes using the
1.141 + * default character encoding.
1.142 + *
1.143 + * @param out An output stream
1.144 + * @param autoFlush A boolean; if true, the <tt>println</tt>,
1.145 + * <tt>printf</tt>, or <tt>format</tt> methods will
1.146 + * flush the output buffer
1.147 + *
1.148 + * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
1.149 + */
1.150 + public PrintWriter(OutputStream out, boolean autoFlush) {
1.151 + this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
1.152 +
1.153 + // save print stream for error propagation
1.154 + if (out instanceof java.io.PrintStream) {
1.155 + psOut = (PrintStream) out;
1.156 + }
1.157 + }
1.158 +
1.159 + /**
1.160 + * Creates a new PrintWriter, without automatic line flushing, with the
1.161 + * specified file name. This convenience constructor creates the necessary
1.162 + * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
1.163 + * which will encode characters using the {@linkplain
1.164 + * java.nio.charset.Charset#defaultCharset() default charset} for this
1.165 + * instance of the Java virtual machine.
1.166 + *
1.167 + * @param fileName
1.168 + * The name of the file to use as the destination of this writer.
1.169 + * If the file exists then it will be truncated to zero size;
1.170 + * otherwise, a new file will be created. The output will be
1.171 + * written to the file and is buffered.
1.172 + *
1.173 + * @throws FileNotFoundException
1.174 + * If the given string does not denote an existing, writable
1.175 + * regular file and a new regular file of that name cannot be
1.176 + * created, or if some other error occurs while opening or
1.177 + * creating the file
1.178 + *
1.179 + * @throws SecurityException
1.180 + * If a security manager is present and {@link
1.181 + * SecurityManager#checkWrite checkWrite(fileName)} denies write
1.182 + * access to the file
1.183 + *
1.184 + * @since 1.5
1.185 + */
1.186 + public PrintWriter(String fileName) throws FileNotFoundException {
1.187 + this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
1.188 + false);
1.189 + }
1.190 +
1.191 + /* Private constructor */
1.192 + private PrintWriter(Charset charset, File file)
1.193 + throws FileNotFoundException
1.194 + {
1.195 + this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
1.196 + false);
1.197 + }
1.198 +
1.199 + /**
1.200 + * Creates a new PrintWriter, without automatic line flushing, with the
1.201 + * specified file name and charset. This convenience constructor creates
1.202 + * the necessary intermediate {@link java.io.OutputStreamWriter
1.203 + * OutputStreamWriter}, which will encode characters using the provided
1.204 + * charset.
1.205 + *
1.206 + * @param fileName
1.207 + * The name of the file to use as the destination of this writer.
1.208 + * If the file exists then it will be truncated to zero size;
1.209 + * otherwise, a new file will be created. The output will be
1.210 + * written to the file and is buffered.
1.211 + *
1.212 + * @param csn
1.213 + * The name of a supported {@linkplain java.nio.charset.Charset
1.214 + * charset}
1.215 + *
1.216 + * @throws FileNotFoundException
1.217 + * If the given string does not denote an existing, writable
1.218 + * regular file and a new regular file of that name cannot be
1.219 + * created, or if some other error occurs while opening or
1.220 + * creating the file
1.221 + *
1.222 + * @throws SecurityException
1.223 + * If a security manager is present and {@link
1.224 + * SecurityManager#checkWrite checkWrite(fileName)} denies write
1.225 + * access to the file
1.226 + *
1.227 + * @throws UnsupportedEncodingException
1.228 + * If the named charset is not supported
1.229 + *
1.230 + * @since 1.5
1.231 + */
1.232 + public PrintWriter(String fileName, String csn)
1.233 + throws FileNotFoundException, UnsupportedEncodingException
1.234 + {
1.235 + this(toCharset(csn), new File(fileName));
1.236 + }
1.237 +
1.238 + /**
1.239 + * Creates a new PrintWriter, without automatic line flushing, with the
1.240 + * specified file. This convenience constructor creates the necessary
1.241 + * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
1.242 + * which will encode characters using the {@linkplain
1.243 + * java.nio.charset.Charset#defaultCharset() default charset} for this
1.244 + * instance of the Java virtual machine.
1.245 + *
1.246 + * @param file
1.247 + * The file to use as the destination of this writer. If the file
1.248 + * exists then it will be truncated to zero size; otherwise, a new
1.249 + * file will be created. The output will be written to the file
1.250 + * and is buffered.
1.251 + *
1.252 + * @throws FileNotFoundException
1.253 + * If the given file object does not denote an existing, writable
1.254 + * regular file and a new regular file of that name cannot be
1.255 + * created, or if some other error occurs while opening or
1.256 + * creating the file
1.257 + *
1.258 + * @throws SecurityException
1.259 + * If a security manager is present and {@link
1.260 + * SecurityManager#checkWrite checkWrite(file.getPath())}
1.261 + * denies write access to the file
1.262 + *
1.263 + * @since 1.5
1.264 + */
1.265 + public PrintWriter(File file) throws FileNotFoundException {
1.266 + this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
1.267 + false);
1.268 + }
1.269 +
1.270 + /**
1.271 + * Creates a new PrintWriter, without automatic line flushing, with the
1.272 + * specified file and charset. This convenience constructor creates the
1.273 + * necessary intermediate {@link java.io.OutputStreamWriter
1.274 + * OutputStreamWriter}, which will encode characters using the provided
1.275 + * charset.
1.276 + *
1.277 + * @param file
1.278 + * The file to use as the destination of this writer. If the file
1.279 + * exists then it will be truncated to zero size; otherwise, a new
1.280 + * file will be created. The output will be written to the file
1.281 + * and is buffered.
1.282 + *
1.283 + * @param csn
1.284 + * The name of a supported {@linkplain java.nio.charset.Charset
1.285 + * charset}
1.286 + *
1.287 + * @throws FileNotFoundException
1.288 + * If the given file object does not denote an existing, writable
1.289 + * regular file and a new regular file of that name cannot be
1.290 + * created, or if some other error occurs while opening or
1.291 + * creating the file
1.292 + *
1.293 + * @throws SecurityException
1.294 + * If a security manager is present and {@link
1.295 + * SecurityManager#checkWrite checkWrite(file.getPath())}
1.296 + * denies write access to the file
1.297 + *
1.298 + * @throws UnsupportedEncodingException
1.299 + * If the named charset is not supported
1.300 + *
1.301 + * @since 1.5
1.302 + */
1.303 + public PrintWriter(File file, String csn)
1.304 + throws FileNotFoundException, UnsupportedEncodingException
1.305 + {
1.306 + this(toCharset(csn), file);
1.307 + }
1.308 +
1.309 + /** Checks to make sure that the stream has not been closed */
1.310 + private void ensureOpen() throws IOException {
1.311 + if (out == null)
1.312 + throw new IOException("Stream closed");
1.313 + }
1.314 +
1.315 + /**
1.316 + * Flushes the stream.
1.317 + * @see #checkError()
1.318 + */
1.319 + public void flush() {
1.320 + try {
1.321 + synchronized (lock) {
1.322 + ensureOpen();
1.323 + out.flush();
1.324 + }
1.325 + }
1.326 + catch (IOException x) {
1.327 + trouble = true;
1.328 + }
1.329 + }
1.330 +
1.331 + /**
1.332 + * Closes the stream and releases any system resources associated
1.333 + * with it. Closing a previously closed stream has no effect.
1.334 + *
1.335 + * @see #checkError()
1.336 + */
1.337 + public void close() {
1.338 + try {
1.339 + synchronized (lock) {
1.340 + if (out == null)
1.341 + return;
1.342 + out.close();
1.343 + out = null;
1.344 + }
1.345 + }
1.346 + catch (IOException x) {
1.347 + trouble = true;
1.348 + }
1.349 + }
1.350 +
1.351 + /**
1.352 + * Flushes the stream if it's not closed and checks its error state.
1.353 + *
1.354 + * @return <code>true</code> if the print stream has encountered an error,
1.355 + * either on the underlying output stream or during a format
1.356 + * conversion.
1.357 + */
1.358 + public boolean checkError() {
1.359 + if (out != null) {
1.360 + flush();
1.361 + }
1.362 + if (out instanceof java.io.PrintWriter) {
1.363 + PrintWriter pw = (PrintWriter) out;
1.364 + return pw.checkError();
1.365 + } else if (psOut != null) {
1.366 + return psOut.checkError();
1.367 + }
1.368 + return trouble;
1.369 + }
1.370 +
1.371 + /**
1.372 + * Indicates that an error has occurred.
1.373 + *
1.374 + * <p> This method will cause subsequent invocations of {@link
1.375 + * #checkError()} to return <tt>true</tt> until {@link
1.376 + * #clearError()} is invoked.
1.377 + */
1.378 + protected void setError() {
1.379 + trouble = true;
1.380 + }
1.381 +
1.382 + /**
1.383 + * Clears the error state of this stream.
1.384 + *
1.385 + * <p> This method will cause subsequent invocations of {@link
1.386 + * #checkError()} to return <tt>false</tt> until another write
1.387 + * operation fails and invokes {@link #setError()}.
1.388 + *
1.389 + * @since 1.6
1.390 + */
1.391 + protected void clearError() {
1.392 + trouble = false;
1.393 + }
1.394 +
1.395 + /*
1.396 + * Exception-catching, synchronized output operations,
1.397 + * which also implement the write() methods of Writer
1.398 + */
1.399 +
1.400 + /**
1.401 + * Writes a single character.
1.402 + * @param c int specifying a character to be written.
1.403 + */
1.404 + public void write(int c) {
1.405 + try {
1.406 + synchronized (lock) {
1.407 + ensureOpen();
1.408 + out.write(c);
1.409 + }
1.410 + }
1.411 + catch (InterruptedIOException x) {
1.412 + Thread.currentThread().interrupt();
1.413 + }
1.414 + catch (IOException x) {
1.415 + trouble = true;
1.416 + }
1.417 + }
1.418 +
1.419 + /**
1.420 + * Writes A Portion of an array of characters.
1.421 + * @param buf Array of characters
1.422 + * @param off Offset from which to start writing characters
1.423 + * @param len Number of characters to write
1.424 + */
1.425 + public void write(char buf[], int off, int len) {
1.426 + try {
1.427 + synchronized (lock) {
1.428 + ensureOpen();
1.429 + out.write(buf, off, len);
1.430 + }
1.431 + }
1.432 + catch (InterruptedIOException x) {
1.433 + Thread.currentThread().interrupt();
1.434 + }
1.435 + catch (IOException x) {
1.436 + trouble = true;
1.437 + }
1.438 + }
1.439 +
1.440 + /**
1.441 + * Writes an array of characters. This method cannot be inherited from the
1.442 + * Writer class because it must suppress I/O exceptions.
1.443 + * @param buf Array of characters to be written
1.444 + */
1.445 + public void write(char buf[]) {
1.446 + write(buf, 0, buf.length);
1.447 + }
1.448 +
1.449 + /**
1.450 + * Writes a portion of a string.
1.451 + * @param s A String
1.452 + * @param off Offset from which to start writing characters
1.453 + * @param len Number of characters to write
1.454 + */
1.455 + public void write(String s, int off, int len) {
1.456 + try {
1.457 + synchronized (lock) {
1.458 + ensureOpen();
1.459 + out.write(s, off, len);
1.460 + }
1.461 + }
1.462 + catch (InterruptedIOException x) {
1.463 + Thread.currentThread().interrupt();
1.464 + }
1.465 + catch (IOException x) {
1.466 + trouble = true;
1.467 + }
1.468 + }
1.469 +
1.470 + /**
1.471 + * Writes a string. This method cannot be inherited from the Writer class
1.472 + * because it must suppress I/O exceptions.
1.473 + * @param s String to be written
1.474 + */
1.475 + public void write(String s) {
1.476 + write(s, 0, s.length());
1.477 + }
1.478 +
1.479 + private void newLine() {
1.480 + try {
1.481 + synchronized (lock) {
1.482 + ensureOpen();
1.483 + out.write(lineSeparator);
1.484 + if (autoFlush)
1.485 + out.flush();
1.486 + }
1.487 + }
1.488 + catch (InterruptedIOException x) {
1.489 + Thread.currentThread().interrupt();
1.490 + }
1.491 + catch (IOException x) {
1.492 + trouble = true;
1.493 + }
1.494 + }
1.495 +
1.496 + /* Methods that do not terminate lines */
1.497 +
1.498 + /**
1.499 + * Prints a boolean value. The string produced by <code>{@link
1.500 + * java.lang.String#valueOf(boolean)}</code> is translated into bytes
1.501 + * according to the platform's default character encoding, and these bytes
1.502 + * are written in exactly the manner of the <code>{@link
1.503 + * #write(int)}</code> method.
1.504 + *
1.505 + * @param b The <code>boolean</code> to be printed
1.506 + */
1.507 + public void print(boolean b) {
1.508 + write(b ? "true" : "false");
1.509 + }
1.510 +
1.511 + /**
1.512 + * Prints a character. The character is translated into one or more bytes
1.513 + * according to the platform's default character encoding, and these bytes
1.514 + * are written in exactly the manner of the <code>{@link
1.515 + * #write(int)}</code> method.
1.516 + *
1.517 + * @param c The <code>char</code> to be printed
1.518 + */
1.519 + public void print(char c) {
1.520 + write(c);
1.521 + }
1.522 +
1.523 + /**
1.524 + * Prints an integer. The string produced by <code>{@link
1.525 + * java.lang.String#valueOf(int)}</code> is translated into bytes according
1.526 + * to the platform's default character encoding, and these bytes are
1.527 + * written in exactly the manner of the <code>{@link #write(int)}</code>
1.528 + * method.
1.529 + *
1.530 + * @param i The <code>int</code> to be printed
1.531 + * @see java.lang.Integer#toString(int)
1.532 + */
1.533 + public void print(int i) {
1.534 + write(String.valueOf(i));
1.535 + }
1.536 +
1.537 + /**
1.538 + * Prints a long integer. The string produced by <code>{@link
1.539 + * java.lang.String#valueOf(long)}</code> is translated into bytes
1.540 + * according to the platform's default character encoding, and these bytes
1.541 + * are written in exactly the manner of the <code>{@link #write(int)}</code>
1.542 + * method.
1.543 + *
1.544 + * @param l The <code>long</code> to be printed
1.545 + * @see java.lang.Long#toString(long)
1.546 + */
1.547 + public void print(long l) {
1.548 + write(String.valueOf(l));
1.549 + }
1.550 +
1.551 + /**
1.552 + * Prints a floating-point number. The string produced by <code>{@link
1.553 + * java.lang.String#valueOf(float)}</code> is translated into bytes
1.554 + * according to the platform's default character encoding, and these bytes
1.555 + * are written in exactly the manner of the <code>{@link #write(int)}</code>
1.556 + * method.
1.557 + *
1.558 + * @param f The <code>float</code> to be printed
1.559 + * @see java.lang.Float#toString(float)
1.560 + */
1.561 + public void print(float f) {
1.562 + write(String.valueOf(f));
1.563 + }
1.564 +
1.565 + /**
1.566 + * Prints a double-precision floating-point number. The string produced by
1.567 + * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
1.568 + * bytes according to the platform's default character encoding, and these
1.569 + * bytes are written in exactly the manner of the <code>{@link
1.570 + * #write(int)}</code> method.
1.571 + *
1.572 + * @param d The <code>double</code> to be printed
1.573 + * @see java.lang.Double#toString(double)
1.574 + */
1.575 + public void print(double d) {
1.576 + write(String.valueOf(d));
1.577 + }
1.578 +
1.579 + /**
1.580 + * Prints an array of characters. The characters are converted into bytes
1.581 + * according to the platform's default character encoding, and these bytes
1.582 + * are written in exactly the manner of the <code>{@link #write(int)}</code>
1.583 + * method.
1.584 + *
1.585 + * @param s The array of chars to be printed
1.586 + *
1.587 + * @throws NullPointerException If <code>s</code> is <code>null</code>
1.588 + */
1.589 + public void print(char s[]) {
1.590 + write(s);
1.591 + }
1.592 +
1.593 + /**
1.594 + * Prints a string. If the argument is <code>null</code> then the string
1.595 + * <code>"null"</code> is printed. Otherwise, the string's characters are
1.596 + * converted into bytes according to the platform's default character
1.597 + * encoding, and these bytes are written in exactly the manner of the
1.598 + * <code>{@link #write(int)}</code> method.
1.599 + *
1.600 + * @param s The <code>String</code> to be printed
1.601 + */
1.602 + public void print(String s) {
1.603 + if (s == null) {
1.604 + s = "null";
1.605 + }
1.606 + write(s);
1.607 + }
1.608 +
1.609 + /**
1.610 + * Prints an object. The string produced by the <code>{@link
1.611 + * java.lang.String#valueOf(Object)}</code> method is translated into bytes
1.612 + * according to the platform's default character encoding, and these bytes
1.613 + * are written in exactly the manner of the <code>{@link #write(int)}</code>
1.614 + * method.
1.615 + *
1.616 + * @param obj The <code>Object</code> to be printed
1.617 + * @see java.lang.Object#toString()
1.618 + */
1.619 + public void print(Object obj) {
1.620 + write(String.valueOf(obj));
1.621 + }
1.622 +
1.623 + /* Methods that do terminate lines */
1.624 +
1.625 + /**
1.626 + * Terminates the current line by writing the line separator string. The
1.627 + * line separator string is defined by the system property
1.628 + * <code>line.separator</code>, and is not necessarily a single newline
1.629 + * character (<code>'\n'</code>).
1.630 + */
1.631 + public void println() {
1.632 + newLine();
1.633 + }
1.634 +
1.635 + /**
1.636 + * Prints a boolean value and then terminates the line. This method behaves
1.637 + * as though it invokes <code>{@link #print(boolean)}</code> and then
1.638 + * <code>{@link #println()}</code>.
1.639 + *
1.640 + * @param x the <code>boolean</code> value to be printed
1.641 + */
1.642 + public void println(boolean x) {
1.643 + synchronized (lock) {
1.644 + print(x);
1.645 + println();
1.646 + }
1.647 + }
1.648 +
1.649 + /**
1.650 + * Prints a character and then terminates the line. This method behaves as
1.651 + * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
1.652 + * #println()}</code>.
1.653 + *
1.654 + * @param x the <code>char</code> value to be printed
1.655 + */
1.656 + public void println(char x) {
1.657 + synchronized (lock) {
1.658 + print(x);
1.659 + println();
1.660 + }
1.661 + }
1.662 +
1.663 + /**
1.664 + * Prints an integer and then terminates the line. This method behaves as
1.665 + * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
1.666 + * #println()}</code>.
1.667 + *
1.668 + * @param x the <code>int</code> value to be printed
1.669 + */
1.670 + public void println(int x) {
1.671 + synchronized (lock) {
1.672 + print(x);
1.673 + println();
1.674 + }
1.675 + }
1.676 +
1.677 + /**
1.678 + * Prints a long integer and then terminates the line. This method behaves
1.679 + * as though it invokes <code>{@link #print(long)}</code> and then
1.680 + * <code>{@link #println()}</code>.
1.681 + *
1.682 + * @param x the <code>long</code> value to be printed
1.683 + */
1.684 + public void println(long x) {
1.685 + synchronized (lock) {
1.686 + print(x);
1.687 + println();
1.688 + }
1.689 + }
1.690 +
1.691 + /**
1.692 + * Prints a floating-point number and then terminates the line. This method
1.693 + * behaves as though it invokes <code>{@link #print(float)}</code> and then
1.694 + * <code>{@link #println()}</code>.
1.695 + *
1.696 + * @param x the <code>float</code> value to be printed
1.697 + */
1.698 + public void println(float x) {
1.699 + synchronized (lock) {
1.700 + print(x);
1.701 + println();
1.702 + }
1.703 + }
1.704 +
1.705 + /**
1.706 + * Prints a double-precision floating-point number and then terminates the
1.707 + * line. This method behaves as though it invokes <code>{@link
1.708 + * #print(double)}</code> and then <code>{@link #println()}</code>.
1.709 + *
1.710 + * @param x the <code>double</code> value to be printed
1.711 + */
1.712 + public void println(double x) {
1.713 + synchronized (lock) {
1.714 + print(x);
1.715 + println();
1.716 + }
1.717 + }
1.718 +
1.719 + /**
1.720 + * Prints an array of characters and then terminates the line. This method
1.721 + * behaves as though it invokes <code>{@link #print(char[])}</code> and then
1.722 + * <code>{@link #println()}</code>.
1.723 + *
1.724 + * @param x the array of <code>char</code> values to be printed
1.725 + */
1.726 + public void println(char x[]) {
1.727 + synchronized (lock) {
1.728 + print(x);
1.729 + println();
1.730 + }
1.731 + }
1.732 +
1.733 + /**
1.734 + * Prints a String and then terminates the line. This method behaves as
1.735 + * though it invokes <code>{@link #print(String)}</code> and then
1.736 + * <code>{@link #println()}</code>.
1.737 + *
1.738 + * @param x the <code>String</code> value to be printed
1.739 + */
1.740 + public void println(String x) {
1.741 + synchronized (lock) {
1.742 + print(x);
1.743 + println();
1.744 + }
1.745 + }
1.746 +
1.747 + /**
1.748 + * Prints an Object and then terminates the line. This method calls
1.749 + * at first String.valueOf(x) to get the printed object's string value,
1.750 + * then behaves as
1.751 + * though it invokes <code>{@link #print(String)}</code> and then
1.752 + * <code>{@link #println()}</code>.
1.753 + *
1.754 + * @param x The <code>Object</code> to be printed.
1.755 + */
1.756 + public void println(Object x) {
1.757 + String s = String.valueOf(x);
1.758 + synchronized (lock) {
1.759 + print(s);
1.760 + println();
1.761 + }
1.762 + }
1.763 +
1.764 + /**
1.765 + * A convenience method to write a formatted string to this writer using
1.766 + * the specified format string and arguments. If automatic flushing is
1.767 + * enabled, calls to this method will flush the output buffer.
1.768 + *
1.769 + * <p> An invocation of this method of the form <tt>out.printf(format,
1.770 + * args)</tt> behaves in exactly the same way as the invocation
1.771 + *
1.772 + * <pre>
1.773 + * out.format(format, args) </pre>
1.774 + *
1.775 + * @param format
1.776 + * A format string as described in <a
1.777 + * href="../util/Formatter.html#syntax">Format string syntax</a>.
1.778 + *
1.779 + * @param args
1.780 + * Arguments referenced by the format specifiers in the format
1.781 + * string. If there are more arguments than format specifiers, the
1.782 + * extra arguments are ignored. The number of arguments is
1.783 + * variable and may be zero. The maximum number of arguments is
1.784 + * limited by the maximum dimension of a Java array as defined by
1.785 + * <cite>The Java™ Virtual Machine Specification</cite>.
1.786 + * The behaviour on a
1.787 + * <tt>null</tt> argument depends on the <a
1.788 + * href="../util/Formatter.html#syntax">conversion</a>.
1.789 + *
1.790 + * @throws IllegalFormatException
1.791 + * If a format string contains an illegal syntax, a format
1.792 + * specifier that is incompatible with the given arguments,
1.793 + * insufficient arguments given the format string, or other
1.794 + * illegal conditions. For specification of all possible
1.795 + * formatting errors, see the <a
1.796 + * href="../util/Formatter.html#detail">Details</a> section of the
1.797 + * formatter class specification.
1.798 + *
1.799 + * @throws NullPointerException
1.800 + * If the <tt>format</tt> is <tt>null</tt>
1.801 + *
1.802 + * @return This writer
1.803 + *
1.804 + * @since 1.5
1.805 + */
1.806 + public PrintWriter printf(String format, Object ... args) {
1.807 + return format(format, args);
1.808 + }
1.809 +
1.810 + /**
1.811 + * A convenience method to write a formatted string to this writer using
1.812 + * the specified format string and arguments. If automatic flushing is
1.813 + * enabled, calls to this method will flush the output buffer.
1.814 + *
1.815 + * <p> An invocation of this method of the form <tt>out.printf(l, format,
1.816 + * args)</tt> behaves in exactly the same way as the invocation
1.817 + *
1.818 + * <pre>
1.819 + * out.format(l, format, args) </pre>
1.820 + *
1.821 + * @param l
1.822 + * The {@linkplain java.util.Locale locale} to apply during
1.823 + * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
1.824 + * is applied.
1.825 + *
1.826 + * @param format
1.827 + * A format string as described in <a
1.828 + * href="../util/Formatter.html#syntax">Format string syntax</a>.
1.829 + *
1.830 + * @param args
1.831 + * Arguments referenced by the format specifiers in the format
1.832 + * string. If there are more arguments than format specifiers, the
1.833 + * extra arguments are ignored. The number of arguments is
1.834 + * variable and may be zero. The maximum number of arguments is
1.835 + * limited by the maximum dimension of a Java array as defined by
1.836 + * <cite>The Java™ Virtual Machine Specification</cite>.
1.837 + * The behaviour on a
1.838 + * <tt>null</tt> argument depends on the <a
1.839 + * href="../util/Formatter.html#syntax">conversion</a>.
1.840 + *
1.841 + * @throws IllegalFormatException
1.842 + * If a format string contains an illegal syntax, a format
1.843 + * specifier that is incompatible with the given arguments,
1.844 + * insufficient arguments given the format string, or other
1.845 + * illegal conditions. For specification of all possible
1.846 + * formatting errors, see the <a
1.847 + * href="../util/Formatter.html#detail">Details</a> section of the
1.848 + * formatter class specification.
1.849 + *
1.850 + * @throws NullPointerException
1.851 + * If the <tt>format</tt> is <tt>null</tt>
1.852 + *
1.853 + * @return This writer
1.854 + *
1.855 + * @since 1.5
1.856 + */
1.857 + public PrintWriter printf(Locale l, String format, Object ... args) {
1.858 + return format(l, format, args);
1.859 + }
1.860 +
1.861 + /**
1.862 + * Writes a formatted string to this writer using the specified format
1.863 + * string and arguments. If automatic flushing is enabled, calls to this
1.864 + * method will flush the output buffer.
1.865 + *
1.866 + * <p> The locale always used is the one returned by {@link
1.867 + * java.util.Locale#getDefault() Locale.getDefault()}, regardless of any
1.868 + * previous invocations of other formatting methods on this object.
1.869 + *
1.870 + * @param format
1.871 + * A format string as described in <a
1.872 + * href="../util/Formatter.html#syntax">Format string syntax</a>.
1.873 + *
1.874 + * @param args
1.875 + * Arguments referenced by the format specifiers in the format
1.876 + * string. If there are more arguments than format specifiers, the
1.877 + * extra arguments are ignored. The number of arguments is
1.878 + * variable and may be zero. The maximum number of arguments is
1.879 + * limited by the maximum dimension of a Java array as defined by
1.880 + * <cite>The Java™ Virtual Machine Specification</cite>.
1.881 + * The behaviour on a
1.882 + * <tt>null</tt> argument depends on the <a
1.883 + * href="../util/Formatter.html#syntax">conversion</a>.
1.884 + *
1.885 + * @throws IllegalFormatException
1.886 + * If a format string contains an illegal syntax, a format
1.887 + * specifier that is incompatible with the given arguments,
1.888 + * insufficient arguments given the format string, or other
1.889 + * illegal conditions. For specification of all possible
1.890 + * formatting errors, see the <a
1.891 + * href="../util/Formatter.html#detail">Details</a> section of the
1.892 + * Formatter class specification.
1.893 + *
1.894 + * @throws NullPointerException
1.895 + * If the <tt>format</tt> is <tt>null</tt>
1.896 + *
1.897 + * @return This writer
1.898 + *
1.899 + * @since 1.5
1.900 + */
1.901 + public PrintWriter format(String format, Object ... args) {
1.902 + try {
1.903 + synchronized (lock) {
1.904 + ensureOpen();
1.905 + if ((formatter == null)
1.906 + || (formatter.locale() != Locale.getDefault()))
1.907 + formatter = new Formatter(this);
1.908 + formatter.format(Locale.getDefault(), format, args);
1.909 + if (autoFlush)
1.910 + out.flush();
1.911 + }
1.912 + } catch (InterruptedIOException x) {
1.913 + Thread.currentThread().interrupt();
1.914 + } catch (IOException x) {
1.915 + trouble = true;
1.916 + }
1.917 + return this;
1.918 + }
1.919 +
1.920 + /**
1.921 + * Writes a formatted string to this writer using the specified format
1.922 + * string and arguments. If automatic flushing is enabled, calls to this
1.923 + * method will flush the output buffer.
1.924 + *
1.925 + * @param l
1.926 + * The {@linkplain java.util.Locale locale} to apply during
1.927 + * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
1.928 + * is applied.
1.929 + *
1.930 + * @param format
1.931 + * A format string as described in <a
1.932 + * href="../util/Formatter.html#syntax">Format string syntax</a>.
1.933 + *
1.934 + * @param args
1.935 + * Arguments referenced by the format specifiers in the format
1.936 + * string. If there are more arguments than format specifiers, the
1.937 + * extra arguments are ignored. The number of arguments is
1.938 + * variable and may be zero. The maximum number of arguments is
1.939 + * limited by the maximum dimension of a Java array as defined by
1.940 + * <cite>The Java™ Virtual Machine Specification</cite>.
1.941 + * The behaviour on a
1.942 + * <tt>null</tt> argument depends on the <a
1.943 + * href="../util/Formatter.html#syntax">conversion</a>.
1.944 + *
1.945 + * @throws IllegalFormatException
1.946 + * If a format string contains an illegal syntax, a format
1.947 + * specifier that is incompatible with the given arguments,
1.948 + * insufficient arguments given the format string, or other
1.949 + * illegal conditions. For specification of all possible
1.950 + * formatting errors, see the <a
1.951 + * href="../util/Formatter.html#detail">Details</a> section of the
1.952 + * formatter class specification.
1.953 + *
1.954 + * @throws NullPointerException
1.955 + * If the <tt>format</tt> is <tt>null</tt>
1.956 + *
1.957 + * @return This writer
1.958 + *
1.959 + * @since 1.5
1.960 + */
1.961 + public PrintWriter format(Locale l, String format, Object ... args) {
1.962 + try {
1.963 + synchronized (lock) {
1.964 + ensureOpen();
1.965 + if ((formatter == null) || (formatter.locale() != l))
1.966 + formatter = new Formatter(this, l);
1.967 + formatter.format(l, format, args);
1.968 + if (autoFlush)
1.969 + out.flush();
1.970 + }
1.971 + } catch (InterruptedIOException x) {
1.972 + Thread.currentThread().interrupt();
1.973 + } catch (IOException x) {
1.974 + trouble = true;
1.975 + }
1.976 + return this;
1.977 + }
1.978 +
1.979 + /**
1.980 + * Appends the specified character sequence to this writer.
1.981 + *
1.982 + * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
1.983 + * behaves in exactly the same way as the invocation
1.984 + *
1.985 + * <pre>
1.986 + * out.write(csq.toString()) </pre>
1.987 + *
1.988 + * <p> Depending on the specification of <tt>toString</tt> for the
1.989 + * character sequence <tt>csq</tt>, the entire sequence may not be
1.990 + * appended. For instance, invoking the <tt>toString</tt> method of a
1.991 + * character buffer will return a subsequence whose content depends upon
1.992 + * the buffer's position and limit.
1.993 + *
1.994 + * @param csq
1.995 + * The character sequence to append. If <tt>csq</tt> is
1.996 + * <tt>null</tt>, then the four characters <tt>"null"</tt> are
1.997 + * appended to this writer.
1.998 + *
1.999 + * @return This writer
1.1000 + *
1.1001 + * @since 1.5
1.1002 + */
1.1003 + public PrintWriter append(CharSequence csq) {
1.1004 + if (csq == null)
1.1005 + write("null");
1.1006 + else
1.1007 + write(csq.toString());
1.1008 + return this;
1.1009 + }
1.1010 +
1.1011 + /**
1.1012 + * Appends a subsequence of the specified character sequence to this writer.
1.1013 + *
1.1014 + * <p> An invocation of this method of the form <tt>out.append(csq, start,
1.1015 + * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
1.1016 + * exactly the same way as the invocation
1.1017 + *
1.1018 + * <pre>
1.1019 + * out.write(csq.subSequence(start, end).toString()) </pre>
1.1020 + *
1.1021 + * @param csq
1.1022 + * The character sequence from which a subsequence will be
1.1023 + * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
1.1024 + * will be appended as if <tt>csq</tt> contained the four
1.1025 + * characters <tt>"null"</tt>.
1.1026 + *
1.1027 + * @param start
1.1028 + * The index of the first character in the subsequence
1.1029 + *
1.1030 + * @param end
1.1031 + * The index of the character following the last character in the
1.1032 + * subsequence
1.1033 + *
1.1034 + * @return This writer
1.1035 + *
1.1036 + * @throws IndexOutOfBoundsException
1.1037 + * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
1.1038 + * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
1.1039 + * <tt>csq.length()</tt>
1.1040 + *
1.1041 + * @since 1.5
1.1042 + */
1.1043 + public PrintWriter append(CharSequence csq, int start, int end) {
1.1044 + CharSequence cs = (csq == null ? "null" : csq);
1.1045 + write(cs.subSequence(start, end).toString());
1.1046 + return this;
1.1047 + }
1.1048 +
1.1049 + /**
1.1050 + * Appends the specified character to this writer.
1.1051 + *
1.1052 + * <p> An invocation of this method of the form <tt>out.append(c)</tt>
1.1053 + * behaves in exactly the same way as the invocation
1.1054 + *
1.1055 + * <pre>
1.1056 + * out.write(c) </pre>
1.1057 + *
1.1058 + * @param c
1.1059 + * The 16-bit character to append
1.1060 + *
1.1061 + * @return This writer
1.1062 + *
1.1063 + * @since 1.5
1.1064 + */
1.1065 + public PrintWriter append(char c) {
1.1066 + write(c);
1.1067 + return this;
1.1068 + }
1.1069 +}