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