rt/emul/compact/src/main/java/java/io/PrintStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Oct 2013 15:02:17 +0200
changeset 1343 802e5d2da9f6
parent 1260 fe3567c7b522
permissions -rw-r--r--
Charset can be compiled now
     1 /*
     2  * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.io;
    27 
    28 import java.nio.charset.Charset;
    29 import java.util.Arrays;
    30 
    31 
    32 /**
    33  * A <code>PrintStream</code> adds functionality to another output stream,
    34  * namely the ability to print representations of various data values
    35  * conveniently.  Two other features are provided as well.  Unlike other output
    36  * streams, a <code>PrintStream</code> never throws an
    37  * <code>IOException</code>; instead, exceptional situations merely set an
    38  * internal flag that can be tested via the <code>checkError</code> method.
    39  * Optionally, a <code>PrintStream</code> can be created so as to flush
    40  * automatically; this means that the <code>flush</code> method is
    41  * automatically invoked after a byte array is written, one of the
    42  * <code>println</code> methods is invoked, or a newline character or byte
    43  * (<code>'\n'</code>) is written.
    44  *
    45  * <p> All characters printed by a <code>PrintStream</code> are converted into
    46  * bytes using the platform's default character encoding.  The <code>{@link
    47  * PrintWriter}</code> class should be used in situations that require writing
    48  * characters rather than bytes.
    49  *
    50  * @author     Frank Yellin
    51  * @author     Mark Reinhold
    52  * @since      JDK1.0
    53  */
    54 
    55 public class PrintStream extends FilterOutputStream
    56     implements Appendable, Closeable
    57 {
    58 
    59     private final boolean autoFlush;
    60     private boolean trouble = false;
    61     private Formatter formatter;
    62 
    63     /**
    64      * Track both the text- and character-output streams, so that their buffers
    65      * can be flushed without flushing the entire stream.
    66      */
    67     private BufferedWriter textOut;
    68     private OutputStreamWriter charOut;
    69 
    70     /**
    71      * requireNonNull is explicitly declared here so as not to create an extra
    72      * dependency on java.util.Objects.requireNonNull. PrintStream is loaded
    73      * early during system initialization.
    74      */
    75     private static <T> T requireNonNull(T obj, String message) {
    76         if (obj == null)
    77             throw new NullPointerException(message);
    78         return obj;
    79     }
    80 
    81     /* Private constructors */
    82     private PrintStream(boolean autoFlush, OutputStream out) {
    83         super(out);
    84         this.autoFlush = autoFlush;
    85         this.charOut = new OutputStreamWriter(this);
    86         this.textOut = new BufferedWriter(charOut);
    87     }
    88     
    89     static final class Formatter {
    90     }
    91     
    92     static Charset toCharset(String ch) throws UnsupportedEncodingException {
    93         if (!"UTF-8".equals(ch)) {
    94             throw new UnsupportedEncodingException();
    95         }
    96         return null;
    97     }
    98 
    99     private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {
   100         super(out);
   101         this.autoFlush = autoFlush;
   102         this.charOut = new OutputStreamWriter(this);
   103         this.textOut = new BufferedWriter(charOut);
   104     }
   105 
   106     /* Variant of the private constructor so that the given charset name
   107      * can be verified before evaluating the OutputStream argument. Used
   108      * by constructors creating a FileOutputStream that also take a
   109      * charset name.
   110      */
   111     private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
   112         throws UnsupportedEncodingException
   113     {
   114         this(autoFlush, out, charset);
   115     }
   116 
   117     /**
   118      * Creates a new print stream.  This stream will not flush automatically.
   119      *
   120      * @param  out        The output stream to which values and objects will be
   121      *                    printed
   122      *
   123      * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
   124      */
   125     public PrintStream(OutputStream out) {
   126         this(out, false);
   127     }
   128 
   129     /**
   130      * Creates a new print stream.
   131      *
   132      * @param  out        The output stream to which values and objects will be
   133      *                    printed
   134      * @param  autoFlush  A boolean; if true, the output buffer will be flushed
   135      *                    whenever a byte array is written, one of the
   136      *                    <code>println</code> methods is invoked, or a newline
   137      *                    character or byte (<code>'\n'</code>) is written
   138      *
   139      * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
   140      */
   141     public PrintStream(OutputStream out, boolean autoFlush) {
   142         this(autoFlush, requireNonNull(out, "Null output stream"));
   143     }
   144 
   145     /**
   146      * Creates a new print stream.
   147      *
   148      * @param  out        The output stream to which values and objects will be
   149      *                    printed
   150      * @param  autoFlush  A boolean; if true, the output buffer will be flushed
   151      *                    whenever a byte array is written, one of the
   152      *                    <code>println</code> methods is invoked, or a newline
   153      *                    character or byte (<code>'\n'</code>) is written
   154      * @param  encoding   The name of a supported
   155      *                    <a href="../lang/package-summary.html#charenc">
   156      *                    character encoding</a>
   157      *
   158      * @throws  UnsupportedEncodingException
   159      *          If the named encoding is not supported
   160      *
   161      * @since  1.4
   162      */
   163     public PrintStream(OutputStream out, boolean autoFlush, String encoding)
   164         throws UnsupportedEncodingException
   165     {
   166         this(autoFlush,
   167              requireNonNull(out, "Null output stream"),
   168              toCharset(encoding));
   169     }
   170 
   171     /**
   172      * Creates a new print stream, without automatic line flushing, with the
   173      * specified file name.  This convenience constructor creates
   174      * the necessary intermediate {@link java.io.OutputStreamWriter
   175      * OutputStreamWriter}, which will encode characters using the
   176      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
   177      * for this instance of the Java virtual machine.
   178      *
   179      * @param  fileName
   180      *         The name of the file to use as the destination of this print
   181      *         stream.  If the file exists, then it will be truncated to
   182      *         zero size; otherwise, a new file will be created.  The output
   183      *         will be written to the file and is buffered.
   184      *
   185      * @throws  FileNotFoundException
   186      *          If the given file object does not denote an existing, writable
   187      *          regular file and a new regular file of that name cannot be
   188      *          created, or if some other error occurs while opening or
   189      *          creating the file
   190      *
   191      * @throws  SecurityException
   192      *          If a security manager is present and {@link
   193      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
   194      *          access to the file
   195      *
   196      * @since  1.5
   197      */
   198     public PrintStream(String fileName) throws FileNotFoundException {
   199         super(null);
   200         throw new FileNotFoundException();
   201     }
   202 
   203     /**
   204      * Creates a new print stream, without automatic line flushing, with the
   205      * specified file name and charset.  This convenience constructor creates
   206      * the necessary intermediate {@link java.io.OutputStreamWriter
   207      * OutputStreamWriter}, which will encode characters using the provided
   208      * charset.
   209      *
   210      * @param  fileName
   211      *         The name of the file to use as the destination of this print
   212      *         stream.  If the file exists, then it will be truncated to
   213      *         zero size; otherwise, a new file will be created.  The output
   214      *         will be written to the file and is buffered.
   215      *
   216      * @param  csn
   217      *         The name of a supported {@linkplain java.nio.charset.Charset
   218      *         charset}
   219      *
   220      * @throws  FileNotFoundException
   221      *          If the given file object does not denote an existing, writable
   222      *          regular file and a new regular file of that name cannot be
   223      *          created, or if some other error occurs while opening or
   224      *          creating the file
   225      *
   226      * @throws  SecurityException
   227      *          If a security manager is present and {@link
   228      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
   229      *          access to the file
   230      *
   231      * @throws  UnsupportedEncodingException
   232      *          If the named charset is not supported
   233      *
   234      * @since  1.5
   235      */
   236     public PrintStream(String fileName, String csn)
   237         throws FileNotFoundException, UnsupportedEncodingException
   238     {
   239         super(null);
   240         throw new FileNotFoundException();
   241     }
   242 
   243     /**
   244      * Creates a new print stream, without automatic line flushing, with the
   245      * specified file.  This convenience constructor creates the necessary
   246      * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
   247      * which will encode characters using the {@linkplain
   248      * java.nio.charset.Charset#defaultCharset() default charset} for this
   249      * instance of the Java virtual machine.
   250      *
   251      * @param  file
   252      *         The file to use as the destination of this print stream.  If the
   253      *         file exists, then it will be truncated to zero size; otherwise,
   254      *         a new file will be created.  The output will be written to the
   255      *         file and is buffered.
   256      *
   257      * @throws  FileNotFoundException
   258      *          If the given file object does not denote an existing, writable
   259      *          regular file and a new regular file of that name cannot be
   260      *          created, or if some other error occurs while opening or
   261      *          creating the file
   262      *
   263      * @throws  SecurityException
   264      *          If a security manager is present and {@link
   265      *          SecurityManager#checkWrite checkWrite(file.getPath())}
   266      *          denies write access to the file
   267      *
   268      * @since  1.5
   269      */
   270     public PrintStream(File file) throws FileNotFoundException {
   271         super(null);
   272         throw new FileNotFoundException();
   273     }
   274 
   275     /**
   276      * Creates a new print stream, without automatic line flushing, with the
   277      * specified file and charset.  This convenience constructor creates
   278      * the necessary intermediate {@link java.io.OutputStreamWriter
   279      * OutputStreamWriter}, which will encode characters using the provided
   280      * charset.
   281      *
   282      * @param  file
   283      *         The file to use as the destination of this print stream.  If the
   284      *         file exists, then it will be truncated to zero size; otherwise,
   285      *         a new file will be created.  The output will be written to the
   286      *         file and is buffered.
   287      *
   288      * @param  csn
   289      *         The name of a supported {@linkplain java.nio.charset.Charset
   290      *         charset}
   291      *
   292      * @throws  FileNotFoundException
   293      *          If the given file object does not denote an existing, writable
   294      *          regular file and a new regular file of that name cannot be
   295      *          created, or if some other error occurs while opening or
   296      *          creating the file
   297      *
   298      * @throws  SecurityException
   299      *          If a security manager is presentand {@link
   300      *          SecurityManager#checkWrite checkWrite(file.getPath())}
   301      *          denies write access to the file
   302      *
   303      * @throws  UnsupportedEncodingException
   304      *          If the named charset is not supported
   305      *
   306      * @since  1.5
   307      */
   308     public PrintStream(File file, String csn)
   309         throws FileNotFoundException, UnsupportedEncodingException
   310     {
   311         super(null);
   312         throw new FileNotFoundException();
   313     }
   314 
   315     /** Check to make sure that the stream has not been closed */
   316     private void ensureOpen() throws IOException {
   317         if (out == null)
   318             throw new IOException("Stream closed");
   319     }
   320 
   321     /**
   322      * Flushes the stream.  This is done by writing any buffered output bytes to
   323      * the underlying output stream and then flushing that stream.
   324      *
   325      * @see        java.io.OutputStream#flush()
   326      */
   327     public void flush() {
   328         synchronized (this) {
   329             try {
   330                 ensureOpen();
   331                 out.flush();
   332             }
   333             catch (IOException x) {
   334                 trouble = true;
   335             }
   336         }
   337     }
   338 
   339     private boolean closing = false; /* To avoid recursive closing */
   340 
   341     /**
   342      * Closes the stream.  This is done by flushing the stream and then closing
   343      * the underlying output stream.
   344      *
   345      * @see        java.io.OutputStream#close()
   346      */
   347     public void close() {
   348         synchronized (this) {
   349             if (! closing) {
   350                 closing = true;
   351                 try {
   352                     textOut.close();
   353                     out.close();
   354                 }
   355                 catch (IOException x) {
   356                     trouble = true;
   357                 }
   358                 textOut = null;
   359                 charOut = null;
   360                 out = null;
   361             }
   362         }
   363     }
   364 
   365     /**
   366      * Flushes the stream and checks its error state. The internal error state
   367      * is set to <code>true</code> when the underlying output stream throws an
   368      * <code>IOException</code> other than <code>InterruptedIOException</code>,
   369      * and when the <code>setError</code> method is invoked.  If an operation
   370      * on the underlying output stream throws an
   371      * <code>InterruptedIOException</code>, then the <code>PrintStream</code>
   372      * converts the exception back into an interrupt by doing:
   373      * <pre>
   374      *     Thread.currentThread().interrupt();
   375      * </pre>
   376      * or the equivalent.
   377      *
   378      * @return <code>true</code> if and only if this stream has encountered an
   379      *         <code>IOException</code> other than
   380      *         <code>InterruptedIOException</code>, or the
   381      *         <code>setError</code> method has been invoked
   382      */
   383     public boolean checkError() {
   384         if (out != null)
   385             flush();
   386         if (out instanceof java.io.PrintStream) {
   387             PrintStream ps = (PrintStream) out;
   388             return ps.checkError();
   389         }
   390         return trouble;
   391     }
   392 
   393     /**
   394      * Sets the error state of the stream to <code>true</code>.
   395      *
   396      * <p> This method will cause subsequent invocations of {@link
   397      * #checkError()} to return <tt>true</tt> until {@link
   398      * #clearError()} is invoked.
   399      *
   400      * @since JDK1.1
   401      */
   402     protected void setError() {
   403         trouble = true;
   404     }
   405 
   406     /**
   407      * Clears the internal error state of this stream.
   408      *
   409      * <p> This method will cause subsequent invocations of {@link
   410      * #checkError()} to return <tt>false</tt> until another write
   411      * operation fails and invokes {@link #setError()}.
   412      *
   413      * @since 1.6
   414      */
   415     protected void clearError() {
   416         trouble = false;
   417     }
   418 
   419     /*
   420      * Exception-catching, synchronized output operations,
   421      * which also implement the write() methods of OutputStream
   422      */
   423 
   424     /**
   425      * Writes the specified byte to this stream.  If the byte is a newline and
   426      * automatic flushing is enabled then the <code>flush</code> method will be
   427      * invoked.
   428      *
   429      * <p> Note that the byte is written as given; to write a character that
   430      * will be translated according to the platform's default character
   431      * encoding, use the <code>print(char)</code> or <code>println(char)</code>
   432      * methods.
   433      *
   434      * @param  b  The byte to be written
   435      * @see #print(char)
   436      * @see #println(char)
   437      */
   438     public void write(int b) {
   439         try {
   440             synchronized (this) {
   441                 ensureOpen();
   442                 out.write(b);
   443                 if ((b == '\n') && autoFlush)
   444                     out.flush();
   445             }
   446         }
   447         catch (InterruptedIOException x) {
   448             Thread.currentThread().interrupt();
   449         }
   450         catch (IOException x) {
   451             trouble = true;
   452         }
   453     }
   454 
   455     /**
   456      * Writes <code>len</code> bytes from the specified byte array starting at
   457      * offset <code>off</code> to this stream.  If automatic flushing is
   458      * enabled then the <code>flush</code> method will be invoked.
   459      *
   460      * <p> Note that the bytes will be written as given; to write characters
   461      * that will be translated according to the platform's default character
   462      * encoding, use the <code>print(char)</code> or <code>println(char)</code>
   463      * methods.
   464      *
   465      * @param  buf   A byte array
   466      * @param  off   Offset from which to start taking bytes
   467      * @param  len   Number of bytes to write
   468      */
   469     public void write(byte buf[], int off, int len) {
   470         try {
   471             synchronized (this) {
   472                 ensureOpen();
   473                 out.write(buf, off, len);
   474                 if (autoFlush)
   475                     out.flush();
   476             }
   477         }
   478         catch (InterruptedIOException x) {
   479             Thread.currentThread().interrupt();
   480         }
   481         catch (IOException x) {
   482             trouble = true;
   483         }
   484     }
   485 
   486     /*
   487      * The following private methods on the text- and character-output streams
   488      * always flush the stream buffers, so that writes to the underlying byte
   489      * stream occur as promptly as with the original PrintStream.
   490      */
   491 
   492     private void write(char buf[]) {
   493         try {
   494             synchronized (this) {
   495                 ensureOpen();
   496                 textOut.write(buf);
   497                 textOut.flushBuffer();
   498                 charOut.flushBuffer();
   499                 if (autoFlush) {
   500                     for (int i = 0; i < buf.length; i++)
   501                         if (buf[i] == '\n')
   502                             out.flush();
   503                 }
   504             }
   505         }
   506         catch (InterruptedIOException x) {
   507             Thread.currentThread().interrupt();
   508         }
   509         catch (IOException x) {
   510             trouble = true;
   511         }
   512     }
   513 
   514     private void write(String s) {
   515         try {
   516             synchronized (this) {
   517                 ensureOpen();
   518                 textOut.write(s);
   519                 textOut.flushBuffer();
   520                 charOut.flushBuffer();
   521                 if (autoFlush && (s.indexOf('\n') >= 0))
   522                     out.flush();
   523             }
   524         }
   525         catch (InterruptedIOException x) {
   526             Thread.currentThread().interrupt();
   527         }
   528         catch (IOException x) {
   529             trouble = true;
   530         }
   531     }
   532 
   533     private void newLine() {
   534         try {
   535             synchronized (this) {
   536                 ensureOpen();
   537                 textOut.newLine();
   538                 textOut.flushBuffer();
   539                 charOut.flushBuffer();
   540                 if (autoFlush)
   541                     out.flush();
   542             }
   543         }
   544         catch (InterruptedIOException x) {
   545             Thread.currentThread().interrupt();
   546         }
   547         catch (IOException x) {
   548             trouble = true;
   549         }
   550     }
   551 
   552     /* Methods that do not terminate lines */
   553 
   554     /**
   555      * Prints a boolean value.  The string produced by <code>{@link
   556      * java.lang.String#valueOf(boolean)}</code> is translated into bytes
   557      * according to the platform's default character encoding, and these bytes
   558      * are written in exactly the manner of the
   559      * <code>{@link #write(int)}</code> method.
   560      *
   561      * @param      b   The <code>boolean</code> to be printed
   562      */
   563     public void print(boolean b) {
   564         write(b ? "true" : "false");
   565     }
   566 
   567     /**
   568      * Prints a character.  The character is translated into one or more bytes
   569      * according to the platform's default character encoding, and these bytes
   570      * are written in exactly the manner of the
   571      * <code>{@link #write(int)}</code> method.
   572      *
   573      * @param      c   The <code>char</code> to be printed
   574      */
   575     public void print(char c) {
   576         write(String.valueOf(c));
   577     }
   578 
   579     /**
   580      * Prints an integer.  The string produced by <code>{@link
   581      * java.lang.String#valueOf(int)}</code> is translated into bytes
   582      * according to the platform's default character encoding, and these bytes
   583      * are written in exactly the manner of the
   584      * <code>{@link #write(int)}</code> method.
   585      *
   586      * @param      i   The <code>int</code> to be printed
   587      * @see        java.lang.Integer#toString(int)
   588      */
   589     public void print(int i) {
   590         write(String.valueOf(i));
   591     }
   592 
   593     /**
   594      * Prints a long integer.  The string produced by <code>{@link
   595      * java.lang.String#valueOf(long)}</code> is translated into bytes
   596      * according to the platform's default character encoding, and these bytes
   597      * are written in exactly the manner of the
   598      * <code>{@link #write(int)}</code> method.
   599      *
   600      * @param      l   The <code>long</code> to be printed
   601      * @see        java.lang.Long#toString(long)
   602      */
   603     public void print(long l) {
   604         write(String.valueOf(l));
   605     }
   606 
   607     /**
   608      * Prints a floating-point number.  The string produced by <code>{@link
   609      * java.lang.String#valueOf(float)}</code> is translated into bytes
   610      * according to the platform's default character encoding, and these bytes
   611      * are written in exactly the manner of the
   612      * <code>{@link #write(int)}</code> method.
   613      *
   614      * @param      f   The <code>float</code> to be printed
   615      * @see        java.lang.Float#toString(float)
   616      */
   617     public void print(float f) {
   618         write(String.valueOf(f));
   619     }
   620 
   621     /**
   622      * Prints a double-precision floating-point number.  The string produced by
   623      * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
   624      * bytes according to the platform's default character encoding, and these
   625      * bytes are written in exactly the manner of the <code>{@link
   626      * #write(int)}</code> method.
   627      *
   628      * @param      d   The <code>double</code> to be printed
   629      * @see        java.lang.Double#toString(double)
   630      */
   631     public void print(double d) {
   632         write(String.valueOf(d));
   633     }
   634 
   635     /**
   636      * Prints an array of characters.  The characters are converted into bytes
   637      * according to the platform's default character encoding, and these bytes
   638      * are written in exactly the manner of the
   639      * <code>{@link #write(int)}</code> method.
   640      *
   641      * @param      s   The array of chars to be printed
   642      *
   643      * @throws  NullPointerException  If <code>s</code> is <code>null</code>
   644      */
   645     public void print(char s[]) {
   646         write(s);
   647     }
   648 
   649     /**
   650      * Prints a string.  If the argument is <code>null</code> then the string
   651      * <code>"null"</code> is printed.  Otherwise, the string's characters are
   652      * converted into bytes according to the platform's default character
   653      * encoding, and these bytes are written in exactly the manner of the
   654      * <code>{@link #write(int)}</code> method.
   655      *
   656      * @param      s   The <code>String</code> to be printed
   657      */
   658     public void print(String s) {
   659         if (s == null) {
   660             s = "null";
   661         }
   662         write(s);
   663     }
   664 
   665     /**
   666      * Prints an object.  The string produced by the <code>{@link
   667      * java.lang.String#valueOf(Object)}</code> method is translated into bytes
   668      * according to the platform's default character encoding, and these bytes
   669      * are written in exactly the manner of the
   670      * <code>{@link #write(int)}</code> method.
   671      *
   672      * @param      obj   The <code>Object</code> to be printed
   673      * @see        java.lang.Object#toString()
   674      */
   675     public void print(Object obj) {
   676         write(String.valueOf(obj));
   677     }
   678 
   679 
   680     /* Methods that do terminate lines */
   681 
   682     /**
   683      * Terminates the current line by writing the line separator string.  The
   684      * line separator string is defined by the system property
   685      * <code>line.separator</code>, and is not necessarily a single newline
   686      * character (<code>'\n'</code>).
   687      */
   688     public void println() {
   689         newLine();
   690     }
   691 
   692     /**
   693      * Prints a boolean and then terminate the line.  This method behaves as
   694      * though it invokes <code>{@link #print(boolean)}</code> and then
   695      * <code>{@link #println()}</code>.
   696      *
   697      * @param x  The <code>boolean</code> to be printed
   698      */
   699     public void println(boolean x) {
   700         synchronized (this) {
   701             print(x);
   702             newLine();
   703         }
   704     }
   705 
   706     /**
   707      * Prints a character and then terminate the line.  This method behaves as
   708      * though it invokes <code>{@link #print(char)}</code> and then
   709      * <code>{@link #println()}</code>.
   710      *
   711      * @param x  The <code>char</code> to be printed.
   712      */
   713     public void println(char x) {
   714         synchronized (this) {
   715             print(x);
   716             newLine();
   717         }
   718     }
   719 
   720     /**
   721      * Prints an integer and then terminate the line.  This method behaves as
   722      * though it invokes <code>{@link #print(int)}</code> and then
   723      * <code>{@link #println()}</code>.
   724      *
   725      * @param x  The <code>int</code> to be printed.
   726      */
   727     public void println(int x) {
   728         synchronized (this) {
   729             print(x);
   730             newLine();
   731         }
   732     }
   733 
   734     /**
   735      * Prints a long and then terminate the line.  This method behaves as
   736      * though it invokes <code>{@link #print(long)}</code> and then
   737      * <code>{@link #println()}</code>.
   738      *
   739      * @param x  a The <code>long</code> to be printed.
   740      */
   741     public void println(long x) {
   742         synchronized (this) {
   743             print(x);
   744             newLine();
   745         }
   746     }
   747 
   748     /**
   749      * Prints a float and then terminate the line.  This method behaves as
   750      * though it invokes <code>{@link #print(float)}</code> and then
   751      * <code>{@link #println()}</code>.
   752      *
   753      * @param x  The <code>float</code> to be printed.
   754      */
   755     public void println(float x) {
   756         synchronized (this) {
   757             print(x);
   758             newLine();
   759         }
   760     }
   761 
   762     /**
   763      * Prints a double and then terminate the line.  This method behaves as
   764      * though it invokes <code>{@link #print(double)}</code> and then
   765      * <code>{@link #println()}</code>.
   766      *
   767      * @param x  The <code>double</code> to be printed.
   768      */
   769     public void println(double x) {
   770         synchronized (this) {
   771             print(x);
   772             newLine();
   773         }
   774     }
   775 
   776     /**
   777      * Prints an array of characters and then terminate the line.  This method
   778      * behaves as though it invokes <code>{@link #print(char[])}</code> and
   779      * then <code>{@link #println()}</code>.
   780      *
   781      * @param x  an array of chars to print.
   782      */
   783     public void println(char x[]) {
   784         synchronized (this) {
   785             print(x);
   786             newLine();
   787         }
   788     }
   789 
   790     /**
   791      * Prints a String and then terminate the line.  This method behaves as
   792      * though it invokes <code>{@link #print(String)}</code> and then
   793      * <code>{@link #println()}</code>.
   794      *
   795      * @param x  The <code>String</code> to be printed.
   796      */
   797     public void println(String x) {
   798         synchronized (this) {
   799             print(x);
   800             newLine();
   801         }
   802     }
   803 
   804     /**
   805      * Prints an Object and then terminate the line.  This method calls
   806      * at first String.valueOf(x) to get the printed object's string value,
   807      * then behaves as
   808      * though it invokes <code>{@link #print(String)}</code> and then
   809      * <code>{@link #println()}</code>.
   810      *
   811      * @param x  The <code>Object</code> to be printed.
   812      */
   813     public void println(Object x) {
   814         String s = String.valueOf(x);
   815         synchronized (this) {
   816             print(s);
   817             newLine();
   818         }
   819     }
   820 
   821 
   822     /**
   823      * A convenience method to write a formatted string to this output stream
   824      * using the specified format string and arguments.
   825      *
   826      * <p> An invocation of this method of the form <tt>out.printf(format,
   827      * args)</tt> behaves in exactly the same way as the invocation
   828      *
   829      * <pre>
   830      *     out.format(format, args) </pre>
   831      *
   832      * @param  format
   833      *         A format string as described in <a
   834      *         href="../util/Formatter.html#syntax">Format string syntax</a>
   835      *
   836      * @param  args
   837      *         Arguments referenced by the format specifiers in the format
   838      *         string.  If there are more arguments than format specifiers, the
   839      *         extra arguments are ignored.  The number of arguments is
   840      *         variable and may be zero.  The maximum number of arguments is
   841      *         limited by the maximum dimension of a Java array as defined by
   842      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
   843      *         The behaviour on a
   844      *         <tt>null</tt> argument depends on the <a
   845      *         href="../util/Formatter.html#syntax">conversion</a>.
   846      *
   847      * @throws  IllegalFormatException
   848      *          If a format string contains an illegal syntax, a format
   849      *          specifier that is incompatible with the given arguments,
   850      *          insufficient arguments given the format string, or other
   851      *          illegal conditions.  For specification of all possible
   852      *          formatting errors, see the <a
   853      *          href="../util/Formatter.html#detail">Details</a> section of the
   854      *          formatter class specification.
   855      *
   856      * @throws  NullPointerException
   857      *          If the <tt>format</tt> is <tt>null</tt>
   858      *
   859      * @return  This output stream
   860      *
   861      * @since  1.5
   862      */
   863     public PrintStream printf(String format, Object ... args) {
   864         append(format).append(Arrays.toString(args));
   865         return this;
   866     }
   867 
   868     /**
   869      * A convenience method to write a formatted string to this output stream
   870      * using the specified format string and arguments.
   871      *
   872      * <p> An invocation of this method of the form <tt>out.printf(l, format,
   873      * args)</tt> behaves in exactly the same way as the invocation
   874      *
   875      * <pre>
   876      *     out.format(l, format, args) </pre>
   877      *
   878      * @param  l
   879      *         The {@linkplain java.util.Locale locale} to apply during
   880      *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
   881      *         is applied.
   882      *
   883      * @param  format
   884      *         A format string as described in <a
   885      *         href="../util/Formatter.html#syntax">Format string syntax</a>
   886      *
   887      * @param  args
   888      *         Arguments referenced by the format specifiers in the format
   889      *         string.  If there are more arguments than format specifiers, the
   890      *         extra arguments are ignored.  The number of arguments is
   891      *         variable and may be zero.  The maximum number of arguments is
   892      *         limited by the maximum dimension of a Java array as defined by
   893      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
   894      *         The behaviour on a
   895      *         <tt>null</tt> argument depends on the <a
   896      *         href="../util/Formatter.html#syntax">conversion</a>.
   897      *
   898      * @throws  IllegalFormatException
   899      *          If a format string contains an illegal syntax, a format
   900      *          specifier that is incompatible with the given arguments,
   901      *          insufficient arguments given the format string, or other
   902      *          illegal conditions.  For specification of all possible
   903      *          formatting errors, see the <a
   904      *          href="../util/Formatter.html#detail">Details</a> section of the
   905      *          formatter class specification.
   906      *
   907      * @throws  NullPointerException
   908      *          If the <tt>format</tt> is <tt>null</tt>
   909      *
   910      * @return  This output stream
   911      *
   912      * @since  1.5
   913      */
   914 //    public PrintStream printf(Locale l, String format, Object ... args) {
   915 //        return format(l, format, args);
   916 //    }
   917 
   918     /**
   919      * Writes a formatted string to this output stream using the specified
   920      * format string and arguments.
   921      *
   922      * <p> The locale always used is the one returned by {@link
   923      * java.util.Locale#getDefault() Locale.getDefault()}, regardless of any
   924      * previous invocations of other formatting methods on this object.
   925      *
   926      * @param  format
   927      *         A format string as described in <a
   928      *         href="../util/Formatter.html#syntax">Format string syntax</a>
   929      *
   930      * @param  args
   931      *         Arguments referenced by the format specifiers in the format
   932      *         string.  If there are more arguments than format specifiers, the
   933      *         extra arguments are ignored.  The number of arguments is
   934      *         variable and may be zero.  The maximum number of arguments is
   935      *         limited by the maximum dimension of a Java array as defined by
   936      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
   937      *         The behaviour on a
   938      *         <tt>null</tt> argument depends on the <a
   939      *         href="../util/Formatter.html#syntax">conversion</a>.
   940      *
   941      * @throws  IllegalFormatException
   942      *          If a format string contains an illegal syntax, a format
   943      *          specifier that is incompatible with the given arguments,
   944      *          insufficient arguments given the format string, or other
   945      *          illegal conditions.  For specification of all possible
   946      *          formatting errors, see the <a
   947      *          href="../util/Formatter.html#detail">Details</a> section of the
   948      *          formatter class specification.
   949      *
   950      * @throws  NullPointerException
   951      *          If the <tt>format</tt> is <tt>null</tt>
   952      *
   953      * @return  This output stream
   954      *
   955      * @since  1.5
   956      */
   957 //    public PrintStream format(String format, Object ... args) {
   958 //        try {
   959 //            synchronized (this) {
   960 //                ensureOpen();
   961 //                if ((formatter == null)
   962 //                    || (formatter.locale() != Locale.getDefault()))
   963 //                    formatter = new Formatter((Appendable) this);
   964 //                formatter.format(Locale.getDefault(), format, args);
   965 //            }
   966 //        } catch (InterruptedIOException x) {
   967 //            Thread.currentThread().interrupt();
   968 //        } catch (IOException x) {
   969 //            trouble = true;
   970 //        }
   971 //        return this;
   972 //    }
   973 
   974     /**
   975      * Writes a formatted string to this output stream using the specified
   976      * format string and arguments.
   977      *
   978      * @param  l
   979      *         The {@linkplain java.util.Locale locale} to apply during
   980      *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
   981      *         is applied.
   982      *
   983      * @param  format
   984      *         A format string as described in <a
   985      *         href="../util/Formatter.html#syntax">Format string syntax</a>
   986      *
   987      * @param  args
   988      *         Arguments referenced by the format specifiers in the format
   989      *         string.  If there are more arguments than format specifiers, the
   990      *         extra arguments are ignored.  The number of arguments is
   991      *         variable and may be zero.  The maximum number of arguments is
   992      *         limited by the maximum dimension of a Java array as defined by
   993      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
   994      *         The behaviour on a
   995      *         <tt>null</tt> argument depends on the <a
   996      *         href="../util/Formatter.html#syntax">conversion</a>.
   997      *
   998      * @throws  IllegalFormatException
   999      *          If a format string contains an illegal syntax, a format
  1000      *          specifier that is incompatible with the given arguments,
  1001      *          insufficient arguments given the format string, or other
  1002      *          illegal conditions.  For specification of all possible
  1003      *          formatting errors, see the <a
  1004      *          href="../util/Formatter.html#detail">Details</a> section of the
  1005      *          formatter class specification.
  1006      *
  1007      * @throws  NullPointerException
  1008      *          If the <tt>format</tt> is <tt>null</tt>
  1009      *
  1010      * @return  This output stream
  1011      *
  1012      * @since  1.5
  1013      */
  1014 ////    public PrintStream format(Locale l, String format, Object ... args) {
  1015 ////        try {
  1016 ////            synchronized (this) {
  1017 ////                ensureOpen();
  1018 ////                if ((formatter == null)
  1019 ////                    || (formatter.locale() != l))
  1020 ////                    formatter = new Formatter(this, l);
  1021 ////                formatter.format(l, format, args);
  1022 ////            }
  1023 ////        } catch (InterruptedIOException x) {
  1024 ////            Thread.currentThread().interrupt();
  1025 ////        } catch (IOException x) {
  1026 ////            trouble = true;
  1027 ////        }
  1028 ////        return this;
  1029 ////    }
  1030 
  1031     /**
  1032      * Appends the specified character sequence to this output stream.
  1033      *
  1034      * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
  1035      * behaves in exactly the same way as the invocation
  1036      *
  1037      * <pre>
  1038      *     out.print(csq.toString()) </pre>
  1039      *
  1040      * <p> Depending on the specification of <tt>toString</tt> for the
  1041      * character sequence <tt>csq</tt>, the entire sequence may not be
  1042      * appended.  For instance, invoking then <tt>toString</tt> method of a
  1043      * character buffer will return a subsequence whose content depends upon
  1044      * the buffer's position and limit.
  1045      *
  1046      * @param  csq
  1047      *         The character sequence to append.  If <tt>csq</tt> is
  1048      *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
  1049      *         appended to this output stream.
  1050      *
  1051      * @return  This output stream
  1052      *
  1053      * @since  1.5
  1054      */
  1055     public PrintStream append(CharSequence csq) {
  1056         if (csq == null)
  1057             print("null");
  1058         else
  1059             print(csq.toString());
  1060         return this;
  1061     }
  1062 
  1063     /**
  1064      * Appends a subsequence of the specified character sequence to this output
  1065      * stream.
  1066      *
  1067      * <p> An invocation of this method of the form <tt>out.append(csq, start,
  1068      * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
  1069      * exactly the same way as the invocation
  1070      *
  1071      * <pre>
  1072      *     out.print(csq.subSequence(start, end).toString()) </pre>
  1073      *
  1074      * @param  csq
  1075      *         The character sequence from which a subsequence will be
  1076      *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
  1077      *         will be appended as if <tt>csq</tt> contained the four
  1078      *         characters <tt>"null"</tt>.
  1079      *
  1080      * @param  start
  1081      *         The index of the first character in the subsequence
  1082      *
  1083      * @param  end
  1084      *         The index of the character following the last character in the
  1085      *         subsequence
  1086      *
  1087      * @return  This output stream
  1088      *
  1089      * @throws  IndexOutOfBoundsException
  1090      *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
  1091      *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
  1092      *          <tt>csq.length()</tt>
  1093      *
  1094      * @since  1.5
  1095      */
  1096     public PrintStream append(CharSequence csq, int start, int end) {
  1097         CharSequence cs = (csq == null ? "null" : csq);
  1098         write(cs.subSequence(start, end).toString());
  1099         return this;
  1100     }
  1101 
  1102     /**
  1103      * Appends the specified character to this output stream.
  1104      *
  1105      * <p> An invocation of this method of the form <tt>out.append(c)</tt>
  1106      * behaves in exactly the same way as the invocation
  1107      *
  1108      * <pre>
  1109      *     out.print(c) </pre>
  1110      *
  1111      * @param  c
  1112      *         The 16-bit character to append
  1113      *
  1114      * @return  This output stream
  1115      *
  1116      * @since  1.5
  1117      */
  1118     public PrintStream append(char c) {
  1119         print(c);
  1120         return this;
  1121     }
  1122 
  1123 }