rt/emul/compact/src/main/java/java/io/PrintWriter.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.io.PrintStream.Formatter;
    29 import java.nio.charset.Charset;
    30 import java.util.Arrays;
    31 
    32 /**
    33  * Prints formatted representations of objects to a text-output stream.  This
    34  * class implements all of the <tt>print</tt> methods found in {@link
    35  * PrintStream}.  It does not contain methods for writing raw bytes, for which
    36  * a program should use unencoded byte streams.
    37  *
    38  * <p> Unlike the {@link PrintStream} class, if automatic flushing is enabled
    39  * it will be done only when one of the <tt>println</tt>, <tt>printf</tt>, or
    40  * <tt>format</tt> methods is invoked, rather than whenever a newline character
    41  * happens to be output.  These methods use the platform's own notion of line
    42  * separator rather than the newline character.
    43  *
    44  * <p> Methods in this class never throw I/O exceptions, although some of its
    45  * constructors may.  The client may inquire as to whether any errors have
    46  * occurred by invoking {@link #checkError checkError()}.
    47  *
    48  * @author      Frank Yellin
    49  * @author      Mark Reinhold
    50  * @since       JDK1.1
    51  */
    52 
    53 public class PrintWriter extends Writer {
    54 
    55     /**
    56      * The underlying character-output stream of this
    57      * <code>PrintWriter</code>.
    58      *
    59      * @since 1.2
    60      */
    61     protected Writer out;
    62 
    63     private final boolean autoFlush;
    64     private boolean trouble = false;
    65     private Formatter formatter;
    66 //    private PrintStream psOut = null;
    67 
    68     /**
    69      * Line separator string.  This is the value of the line.separator
    70      * property at the moment that the stream was created.
    71      */
    72     private final String lineSeparator;
    73 
    74     /**
    75      * Returns a charset object for the given charset name.
    76      * @throws NullPointerException          is csn is null
    77      * @throws UnsupportedEncodingException  if the charset is not supported
    78      */
    79     private static Charset toCharset(String csn)
    80         throws UnsupportedEncodingException
    81     {
    82         return PrintStream.toCharset(csn);
    83     }
    84 
    85     /**
    86      * Creates a new PrintWriter, without automatic line flushing.
    87      *
    88      * @param  out        A character-output stream
    89      */
    90     public PrintWriter (Writer out) {
    91         this(out, false);
    92     }
    93 
    94     /**
    95      * Creates a new PrintWriter.
    96      *
    97      * @param  out        A character-output stream
    98      * @param  autoFlush  A boolean; if true, the <tt>println</tt>,
    99      *                    <tt>printf</tt>, or <tt>format</tt> methods will
   100      *                    flush the output buffer
   101      */
   102     public PrintWriter(Writer out,
   103                        boolean autoFlush) {
   104         super(out);
   105         this.out = out;
   106         this.autoFlush = autoFlush;
   107         lineSeparator = "\n";
   108     }
   109 
   110     /**
   111      * Creates a new PrintWriter, without automatic line flushing, from an
   112      * existing OutputStream.  This convenience constructor creates the
   113      * necessary intermediate OutputStreamWriter, which will convert characters
   114      * into bytes using the default character encoding.
   115      *
   116      * @param  out        An output stream
   117      *
   118      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
   119      */
   120     public PrintWriter(OutputStream out) {
   121         this(out, false);
   122     }
   123 
   124     /**
   125      * Creates a new PrintWriter from an existing OutputStream.  This
   126      * convenience constructor creates the necessary intermediate
   127      * OutputStreamWriter, which will convert characters into bytes using the
   128      * default character encoding.
   129      *
   130      * @param  out        An output stream
   131      * @param  autoFlush  A boolean; if true, the <tt>println</tt>,
   132      *                    <tt>printf</tt>, or <tt>format</tt> methods will
   133      *                    flush the output buffer
   134      *
   135      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
   136      */
   137     public PrintWriter(OutputStream out, boolean autoFlush) {
   138         this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
   139 
   140         // save print stream for error propagation
   141 //        if (out instanceof java.io.PrintStream) {
   142 //            psOut = (PrintStream) out;
   143 //        }
   144     }
   145 
   146     /**
   147      * Creates a new PrintWriter, without automatic line flushing, with the
   148      * specified file name.  This convenience constructor creates the necessary
   149      * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
   150      * which will encode characters using the {@linkplain
   151      * java.nio.charset.Charset#defaultCharset() default charset} for this
   152      * instance of the Java virtual machine.
   153      *
   154      * @param  fileName
   155      *         The name of the file to use as the destination of this writer.
   156      *         If the file exists then it will be truncated to zero size;
   157      *         otherwise, a new file will be created.  The output will be
   158      *         written to the file and is buffered.
   159      *
   160      * @throws  FileNotFoundException
   161      *          If the given string does not denote an existing, writable
   162      *          regular file and a new regular file of that name cannot be
   163      *          created, or if some other error occurs while opening or
   164      *          creating the file
   165      *
   166      * @throws  SecurityException
   167      *          If a security manager is present and {@link
   168      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
   169      *          access to the file
   170      *
   171      * @since  1.5
   172      */
   173     public PrintWriter(String fileName) throws FileNotFoundException {
   174         super();
   175         throw new FileNotFoundException();
   176     }
   177 
   178     /* Private constructor */
   179     private PrintWriter(Charset charset, File file)
   180         throws FileNotFoundException
   181     {
   182         super();
   183         throw new FileNotFoundException();
   184     }
   185 
   186     /**
   187      * Creates a new PrintWriter, without automatic line flushing, with the
   188      * specified file name and charset.  This convenience constructor creates
   189      * the necessary intermediate {@link java.io.OutputStreamWriter
   190      * OutputStreamWriter}, which will encode characters using the provided
   191      * charset.
   192      *
   193      * @param  fileName
   194      *         The name of the file to use as the destination of this writer.
   195      *         If the file exists then it will be truncated to zero size;
   196      *         otherwise, a new file will be created.  The output will be
   197      *         written to the file and is buffered.
   198      *
   199      * @param  csn
   200      *         The name of a supported {@linkplain java.nio.charset.Charset
   201      *         charset}
   202      *
   203      * @throws  FileNotFoundException
   204      *          If the given string does not denote an existing, writable
   205      *          regular file and a new regular file of that name cannot be
   206      *          created, or if some other error occurs while opening or
   207      *          creating the file
   208      *
   209      * @throws  SecurityException
   210      *          If a security manager is present and {@link
   211      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
   212      *          access to the file
   213      *
   214      * @throws  UnsupportedEncodingException
   215      *          If the named charset is not supported
   216      *
   217      * @since  1.5
   218      */
   219     public PrintWriter(String fileName, String csn)
   220         throws FileNotFoundException, UnsupportedEncodingException
   221     {
   222         this(toCharset(csn), new File(fileName));
   223     }
   224 
   225     /**
   226      * Creates a new PrintWriter, without automatic line flushing, with the
   227      * specified file.  This convenience constructor creates the necessary
   228      * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
   229      * which will encode characters using the {@linkplain
   230      * java.nio.charset.Charset#defaultCharset() default charset} for this
   231      * instance of the Java virtual machine.
   232      *
   233      * @param  file
   234      *         The file to use as the destination of this writer.  If the file
   235      *         exists then it will be truncated to zero size; otherwise, a new
   236      *         file will be created.  The output will be written to the file
   237      *         and is buffered.
   238      *
   239      * @throws  FileNotFoundException
   240      *          If the given file object does not denote an existing, writable
   241      *          regular file and a new regular file of that name cannot be
   242      *          created, or if some other error occurs while opening or
   243      *          creating the file
   244      *
   245      * @throws  SecurityException
   246      *          If a security manager is present and {@link
   247      *          SecurityManager#checkWrite checkWrite(file.getPath())}
   248      *          denies write access to the file
   249      *
   250      * @since  1.5
   251      */
   252     public PrintWriter(File file) throws FileNotFoundException {
   253         super();
   254         throw new FileNotFoundException();
   255     }
   256 
   257     /**
   258      * Creates a new PrintWriter, without automatic line flushing, with the
   259      * specified file and charset.  This convenience constructor creates the
   260      * necessary intermediate {@link java.io.OutputStreamWriter
   261      * OutputStreamWriter}, which will encode characters using the provided
   262      * charset.
   263      *
   264      * @param  file
   265      *         The file to use as the destination of this writer.  If the file
   266      *         exists then it will be truncated to zero size; otherwise, a new
   267      *         file will be created.  The output will be written to the file
   268      *         and is buffered.
   269      *
   270      * @param  csn
   271      *         The name of a supported {@linkplain java.nio.charset.Charset
   272      *         charset}
   273      *
   274      * @throws  FileNotFoundException
   275      *          If the given file object does not denote an existing, writable
   276      *          regular file and a new regular file of that name cannot be
   277      *          created, or if some other error occurs while opening or
   278      *          creating the file
   279      *
   280      * @throws  SecurityException
   281      *          If a security manager is present and {@link
   282      *          SecurityManager#checkWrite checkWrite(file.getPath())}
   283      *          denies write access to the file
   284      *
   285      * @throws  UnsupportedEncodingException
   286      *          If the named charset is not supported
   287      *
   288      * @since  1.5
   289      */
   290     public PrintWriter(File file, String csn)
   291         throws FileNotFoundException, UnsupportedEncodingException
   292     {
   293         this(toCharset(csn), file);
   294     }
   295 
   296     /** Checks to make sure that the stream has not been closed */
   297     private void ensureOpen() throws IOException {
   298         if (out == null)
   299             throw new IOException("Stream closed");
   300     }
   301 
   302     /**
   303      * Flushes the stream.
   304      * @see #checkError()
   305      */
   306     public void flush() {
   307         try {
   308             synchronized (lock) {
   309                 ensureOpen();
   310                 out.flush();
   311             }
   312         }
   313         catch (IOException x) {
   314             trouble = true;
   315         }
   316     }
   317 
   318     /**
   319      * Closes the stream and releases any system resources associated
   320      * with it. Closing a previously closed stream has no effect.
   321      *
   322      * @see #checkError()
   323      */
   324     public void close() {
   325         try {
   326             synchronized (lock) {
   327                 if (out == null)
   328                     return;
   329                 out.close();
   330                 out = null;
   331             }
   332         }
   333         catch (IOException x) {
   334             trouble = true;
   335         }
   336     }
   337 
   338     /**
   339      * Flushes the stream if it's not closed and checks its error state.
   340      *
   341      * @return <code>true</code> if the print stream has encountered an error,
   342      *          either on the underlying output stream or during a format
   343      *          conversion.
   344      */
   345     public boolean checkError() {
   346         if (out != null) {
   347             flush();
   348         }
   349         if (out instanceof java.io.PrintWriter) {
   350             PrintWriter pw = (PrintWriter) out;
   351             return pw.checkError();
   352         } else 
   353 //        if (psOut != null) {
   354 //            return psOut.checkError();
   355 //        }
   356         return trouble;
   357     }
   358 
   359     /**
   360      * Indicates that an error has occurred.
   361      *
   362      * <p> This method will cause subsequent invocations of {@link
   363      * #checkError()} to return <tt>true</tt> until {@link
   364      * #clearError()} is invoked.
   365      */
   366     protected void setError() {
   367         trouble = true;
   368     }
   369 
   370     /**
   371      * Clears the error state of this stream.
   372      *
   373      * <p> This method will cause subsequent invocations of {@link
   374      * #checkError()} to return <tt>false</tt> until another write
   375      * operation fails and invokes {@link #setError()}.
   376      *
   377      * @since 1.6
   378      */
   379     protected void clearError() {
   380         trouble = false;
   381     }
   382 
   383     /*
   384      * Exception-catching, synchronized output operations,
   385      * which also implement the write() methods of Writer
   386      */
   387 
   388     /**
   389      * Writes a single character.
   390      * @param c int specifying a character to be written.
   391      */
   392     public void write(int c) {
   393         try {
   394             synchronized (lock) {
   395                 ensureOpen();
   396                 out.write(c);
   397             }
   398         }
   399         catch (InterruptedIOException x) {
   400             Thread.currentThread().interrupt();
   401         }
   402         catch (IOException x) {
   403             trouble = true;
   404         }
   405     }
   406 
   407     /**
   408      * Writes A Portion of an array of characters.
   409      * @param buf Array of characters
   410      * @param off Offset from which to start writing characters
   411      * @param len Number of characters to write
   412      */
   413     public void write(char buf[], int off, int len) {
   414         try {
   415             synchronized (lock) {
   416                 ensureOpen();
   417                 out.write(buf, off, len);
   418             }
   419         }
   420         catch (InterruptedIOException x) {
   421             Thread.currentThread().interrupt();
   422         }
   423         catch (IOException x) {
   424             trouble = true;
   425         }
   426     }
   427 
   428     /**
   429      * Writes an array of characters.  This method cannot be inherited from the
   430      * Writer class because it must suppress I/O exceptions.
   431      * @param buf Array of characters to be written
   432      */
   433     public void write(char buf[]) {
   434         write(buf, 0, buf.length);
   435     }
   436 
   437     /**
   438      * Writes a portion of a string.
   439      * @param s A String
   440      * @param off Offset from which to start writing characters
   441      * @param len Number of characters to write
   442      */
   443     public void write(String s, int off, int len) {
   444         try {
   445             synchronized (lock) {
   446                 ensureOpen();
   447                 out.write(s, off, len);
   448             }
   449         }
   450         catch (InterruptedIOException x) {
   451             Thread.currentThread().interrupt();
   452         }
   453         catch (IOException x) {
   454             trouble = true;
   455         }
   456     }
   457 
   458     /**
   459      * Writes a string.  This method cannot be inherited from the Writer class
   460      * because it must suppress I/O exceptions.
   461      * @param s String to be written
   462      */
   463     public void write(String s) {
   464         write(s, 0, s.length());
   465     }
   466 
   467     private void newLine() {
   468         try {
   469             synchronized (lock) {
   470                 ensureOpen();
   471                 out.write(lineSeparator);
   472                 if (autoFlush)
   473                     out.flush();
   474             }
   475         }
   476         catch (InterruptedIOException x) {
   477             Thread.currentThread().interrupt();
   478         }
   479         catch (IOException x) {
   480             trouble = true;
   481         }
   482     }
   483 
   484     /* Methods that do not terminate lines */
   485 
   486     /**
   487      * Prints a boolean value.  The string produced by <code>{@link
   488      * java.lang.String#valueOf(boolean)}</code> is translated into bytes
   489      * according to the platform's default character encoding, and these bytes
   490      * are written in exactly the manner of the <code>{@link
   491      * #write(int)}</code> method.
   492      *
   493      * @param      b   The <code>boolean</code> to be printed
   494      */
   495     public void print(boolean b) {
   496         write(b ? "true" : "false");
   497     }
   498 
   499     /**
   500      * Prints a character.  The character is translated into one or more bytes
   501      * according to the platform's default character encoding, and these bytes
   502      * are written in exactly the manner of the <code>{@link
   503      * #write(int)}</code> method.
   504      *
   505      * @param      c   The <code>char</code> to be printed
   506      */
   507     public void print(char c) {
   508         write(c);
   509     }
   510 
   511     /**
   512      * Prints an integer.  The string produced by <code>{@link
   513      * java.lang.String#valueOf(int)}</code> is translated into bytes according
   514      * to the platform's default character encoding, and these bytes are
   515      * written in exactly the manner of the <code>{@link #write(int)}</code>
   516      * method.
   517      *
   518      * @param      i   The <code>int</code> to be printed
   519      * @see        java.lang.Integer#toString(int)
   520      */
   521     public void print(int i) {
   522         write(String.valueOf(i));
   523     }
   524 
   525     /**
   526      * Prints a long integer.  The string produced by <code>{@link
   527      * java.lang.String#valueOf(long)}</code> is translated into bytes
   528      * according to the platform's default character encoding, and these bytes
   529      * are written in exactly the manner of the <code>{@link #write(int)}</code>
   530      * method.
   531      *
   532      * @param      l   The <code>long</code> to be printed
   533      * @see        java.lang.Long#toString(long)
   534      */
   535     public void print(long l) {
   536         write(String.valueOf(l));
   537     }
   538 
   539     /**
   540      * Prints a floating-point number.  The string produced by <code>{@link
   541      * java.lang.String#valueOf(float)}</code> is translated into bytes
   542      * according to the platform's default character encoding, and these bytes
   543      * are written in exactly the manner of the <code>{@link #write(int)}</code>
   544      * method.
   545      *
   546      * @param      f   The <code>float</code> to be printed
   547      * @see        java.lang.Float#toString(float)
   548      */
   549     public void print(float f) {
   550         write(String.valueOf(f));
   551     }
   552 
   553     /**
   554      * Prints a double-precision floating-point number.  The string produced by
   555      * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
   556      * bytes according to the platform's default character encoding, and these
   557      * bytes are written in exactly the manner of the <code>{@link
   558      * #write(int)}</code> method.
   559      *
   560      * @param      d   The <code>double</code> to be printed
   561      * @see        java.lang.Double#toString(double)
   562      */
   563     public void print(double d) {
   564         write(String.valueOf(d));
   565     }
   566 
   567     /**
   568      * Prints an array of characters.  The characters are converted into bytes
   569      * according to the platform's default character encoding, and these bytes
   570      * are written in exactly the manner of the <code>{@link #write(int)}</code>
   571      * method.
   572      *
   573      * @param      s   The array of chars to be printed
   574      *
   575      * @throws  NullPointerException  If <code>s</code> is <code>null</code>
   576      */
   577     public void print(char s[]) {
   578         write(s);
   579     }
   580 
   581     /**
   582      * Prints a string.  If the argument is <code>null</code> then the string
   583      * <code>"null"</code> is printed.  Otherwise, the string's characters are
   584      * converted into bytes according to the platform's default character
   585      * encoding, and these bytes are written in exactly the manner of the
   586      * <code>{@link #write(int)}</code> method.
   587      *
   588      * @param      s   The <code>String</code> to be printed
   589      */
   590     public void print(String s) {
   591         if (s == null) {
   592             s = "null";
   593         }
   594         write(s);
   595     }
   596 
   597     /**
   598      * Prints an object.  The string produced by the <code>{@link
   599      * java.lang.String#valueOf(Object)}</code> method is translated into bytes
   600      * according to the platform's default character encoding, and these bytes
   601      * are written in exactly the manner of the <code>{@link #write(int)}</code>
   602      * method.
   603      *
   604      * @param      obj   The <code>Object</code> to be printed
   605      * @see        java.lang.Object#toString()
   606      */
   607     public void print(Object obj) {
   608         write(String.valueOf(obj));
   609     }
   610 
   611     /* Methods that do terminate lines */
   612 
   613     /**
   614      * Terminates the current line by writing the line separator string.  The
   615      * line separator string is defined by the system property
   616      * <code>line.separator</code>, and is not necessarily a single newline
   617      * character (<code>'\n'</code>).
   618      */
   619     public void println() {
   620         newLine();
   621     }
   622 
   623     /**
   624      * Prints a boolean value and then terminates the line.  This method behaves
   625      * as though it invokes <code>{@link #print(boolean)}</code> and then
   626      * <code>{@link #println()}</code>.
   627      *
   628      * @param x the <code>boolean</code> value to be printed
   629      */
   630     public void println(boolean x) {
   631         synchronized (lock) {
   632             print(x);
   633             println();
   634         }
   635     }
   636 
   637     /**
   638      * Prints a character and then terminates the line.  This method behaves as
   639      * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
   640      * #println()}</code>.
   641      *
   642      * @param x the <code>char</code> value to be printed
   643      */
   644     public void println(char x) {
   645         synchronized (lock) {
   646             print(x);
   647             println();
   648         }
   649     }
   650 
   651     /**
   652      * Prints an integer and then terminates the line.  This method behaves as
   653      * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
   654      * #println()}</code>.
   655      *
   656      * @param x the <code>int</code> value to be printed
   657      */
   658     public void println(int x) {
   659         synchronized (lock) {
   660             print(x);
   661             println();
   662         }
   663     }
   664 
   665     /**
   666      * Prints a long integer and then terminates the line.  This method behaves
   667      * as though it invokes <code>{@link #print(long)}</code> and then
   668      * <code>{@link #println()}</code>.
   669      *
   670      * @param x the <code>long</code> value to be printed
   671      */
   672     public void println(long x) {
   673         synchronized (lock) {
   674             print(x);
   675             println();
   676         }
   677     }
   678 
   679     /**
   680      * Prints a floating-point number and then terminates the line.  This method
   681      * behaves as though it invokes <code>{@link #print(float)}</code> and then
   682      * <code>{@link #println()}</code>.
   683      *
   684      * @param x the <code>float</code> value to be printed
   685      */
   686     public void println(float x) {
   687         synchronized (lock) {
   688             print(x);
   689             println();
   690         }
   691     }
   692 
   693     /**
   694      * Prints a double-precision floating-point number and then terminates the
   695      * line.  This method behaves as though it invokes <code>{@link
   696      * #print(double)}</code> and then <code>{@link #println()}</code>.
   697      *
   698      * @param x the <code>double</code> value to be printed
   699      */
   700     public void println(double x) {
   701         synchronized (lock) {
   702             print(x);
   703             println();
   704         }
   705     }
   706 
   707     /**
   708      * Prints an array of characters and then terminates the line.  This method
   709      * behaves as though it invokes <code>{@link #print(char[])}</code> and then
   710      * <code>{@link #println()}</code>.
   711      *
   712      * @param x the array of <code>char</code> values to be printed
   713      */
   714     public void println(char x[]) {
   715         synchronized (lock) {
   716             print(x);
   717             println();
   718         }
   719     }
   720 
   721     /**
   722      * Prints a String and then terminates the line.  This method behaves as
   723      * though it invokes <code>{@link #print(String)}</code> and then
   724      * <code>{@link #println()}</code>.
   725      *
   726      * @param x the <code>String</code> value to be printed
   727      */
   728     public void println(String x) {
   729         synchronized (lock) {
   730             print(x);
   731             println();
   732         }
   733     }
   734 
   735     /**
   736      * Prints an Object and then terminates the line.  This method calls
   737      * at first String.valueOf(x) to get the printed object's string value,
   738      * then behaves as
   739      * though it invokes <code>{@link #print(String)}</code> and then
   740      * <code>{@link #println()}</code>.
   741      *
   742      * @param x  The <code>Object</code> to be printed.
   743      */
   744     public void println(Object x) {
   745         String s = String.valueOf(x);
   746         synchronized (lock) {
   747             print(s);
   748             println();
   749         }
   750     }
   751 
   752     /**
   753      * A convenience method to write a formatted string to this writer using
   754      * the specified format string and arguments.  If automatic flushing is
   755      * enabled, calls to this method will flush the output buffer.
   756      *
   757      * <p> An invocation of this method of the form <tt>out.printf(format,
   758      * args)</tt> behaves in exactly the same way as the invocation
   759      *
   760      * <pre>
   761      *     out.format(format, args) </pre>
   762      *
   763      * @param  format
   764      *         A format string as described in <a
   765      *         href="../util/Formatter.html#syntax">Format string syntax</a>.
   766      *
   767      * @param  args
   768      *         Arguments referenced by the format specifiers in the format
   769      *         string.  If there are more arguments than format specifiers, the
   770      *         extra arguments are ignored.  The number of arguments is
   771      *         variable and may be zero.  The maximum number of arguments is
   772      *         limited by the maximum dimension of a Java array as defined by
   773      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
   774      *         The behaviour on a
   775      *         <tt>null</tt> argument depends on the <a
   776      *         href="../util/Formatter.html#syntax">conversion</a>.
   777      *
   778      * @throws  IllegalFormatException
   779      *          If a format string contains an illegal syntax, a format
   780      *          specifier that is incompatible with the given arguments,
   781      *          insufficient arguments given the format string, or other
   782      *          illegal conditions.  For specification of all possible
   783      *          formatting errors, see the <a
   784      *          href="../util/Formatter.html#detail">Details</a> section of the
   785      *          formatter class specification.
   786      *
   787      * @throws  NullPointerException
   788      *          If the <tt>format</tt> is <tt>null</tt>
   789      *
   790      * @return  This writer
   791      *
   792      * @since  1.5
   793      */
   794     public PrintWriter printf(String format, Object ... args) {
   795         return format(format, args);
   796     }
   797 
   798     /**
   799      * A convenience method to write a formatted string to this writer using
   800      * the specified format string and arguments.  If automatic flushing is
   801      * enabled, calls to this method will flush the output buffer.
   802      *
   803      * <p> An invocation of this method of the form <tt>out.printf(l, format,
   804      * args)</tt> behaves in exactly the same way as the invocation
   805      *
   806      * <pre>
   807      *     out.format(l, format, args) </pre>
   808      *
   809      * @param  l
   810      *         The {@linkplain java.util.Locale locale} to apply during
   811      *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
   812      *         is applied.
   813      *
   814      * @param  format
   815      *         A format string as described in <a
   816      *         href="../util/Formatter.html#syntax">Format string syntax</a>.
   817      *
   818      * @param  args
   819      *         Arguments referenced by the format specifiers in the format
   820      *         string.  If there are more arguments than format specifiers, the
   821      *         extra arguments are ignored.  The number of arguments is
   822      *         variable and may be zero.  The maximum number of arguments is
   823      *         limited by the maximum dimension of a Java array as defined by
   824      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
   825      *         The behaviour on a
   826      *         <tt>null</tt> argument depends on the <a
   827      *         href="../util/Formatter.html#syntax">conversion</a>.
   828      *
   829      * @throws  IllegalFormatException
   830      *          If a format string contains an illegal syntax, a format
   831      *          specifier that is incompatible with the given arguments,
   832      *          insufficient arguments given the format string, or other
   833      *          illegal conditions.  For specification of all possible
   834      *          formatting errors, see the <a
   835      *          href="../util/Formatter.html#detail">Details</a> section of the
   836      *          formatter class specification.
   837      *
   838      * @throws  NullPointerException
   839      *          If the <tt>format</tt> is <tt>null</tt>
   840      *
   841      * @return  This writer
   842      *
   843      * @since  1.5
   844      */
   845 //    public PrintWriter printf(Locale l, String format, Object ... args) {
   846 //        return format(l, format, args);
   847 //    }
   848 
   849     /**
   850      * Writes a formatted string to this writer using the specified format
   851      * string and arguments.  If automatic flushing is enabled, calls to this
   852      * method will flush the output buffer.
   853      *
   854      * <p> The locale always used is the one returned by {@link
   855      * java.util.Locale#getDefault() Locale.getDefault()}, regardless of any
   856      * previous invocations of other formatting methods on this object.
   857      *
   858      * @param  format
   859      *         A format string as described in <a
   860      *         href="../util/Formatter.html#syntax">Format string syntax</a>.
   861      *
   862      * @param  args
   863      *         Arguments referenced by the format specifiers in the format
   864      *         string.  If there are more arguments than format specifiers, the
   865      *         extra arguments are ignored.  The number of arguments is
   866      *         variable and may be zero.  The maximum number of arguments is
   867      *         limited by the maximum dimension of a Java array as defined by
   868      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
   869      *         The behaviour on a
   870      *         <tt>null</tt> argument depends on the <a
   871      *         href="../util/Formatter.html#syntax">conversion</a>.
   872      *
   873      * @throws  IllegalFormatException
   874      *          If a format string contains an illegal syntax, a format
   875      *          specifier that is incompatible with the given arguments,
   876      *          insufficient arguments given the format string, or other
   877      *          illegal conditions.  For specification of all possible
   878      *          formatting errors, see the <a
   879      *          href="../util/Formatter.html#detail">Details</a> section of the
   880      *          Formatter class specification.
   881      *
   882      * @throws  NullPointerException
   883      *          If the <tt>format</tt> is <tt>null</tt>
   884      *
   885      * @return  This writer
   886      *
   887      * @since  1.5
   888      */
   889     public PrintWriter format(String format, Object ... args) {
   890         append(format).append(Arrays.toString(args));
   891         return this;
   892     }
   893 
   894     /**
   895      * Writes a formatted string to this writer using the specified format
   896      * string and arguments.  If automatic flushing is enabled, calls to this
   897      * method will flush the output buffer.
   898      *
   899      * @param  l
   900      *         The {@linkplain java.util.Locale locale} to apply during
   901      *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
   902      *         is applied.
   903      *
   904      * @param  format
   905      *         A format string as described in <a
   906      *         href="../util/Formatter.html#syntax">Format string syntax</a>.
   907      *
   908      * @param  args
   909      *         Arguments referenced by the format specifiers in the format
   910      *         string.  If there are more arguments than format specifiers, the
   911      *         extra arguments are ignored.  The number of arguments is
   912      *         variable and may be zero.  The maximum number of arguments is
   913      *         limited by the maximum dimension of a Java array as defined by
   914      *         <cite>The Java&trade; Virtual Machine Specification</cite>.
   915      *         The behaviour on a
   916      *         <tt>null</tt> argument depends on the <a
   917      *         href="../util/Formatter.html#syntax">conversion</a>.
   918      *
   919      * @throws  IllegalFormatException
   920      *          If a format string contains an illegal syntax, a format
   921      *          specifier that is incompatible with the given arguments,
   922      *          insufficient arguments given the format string, or other
   923      *          illegal conditions.  For specification of all possible
   924      *          formatting errors, see the <a
   925      *          href="../util/Formatter.html#detail">Details</a> section of the
   926      *          formatter class specification.
   927      *
   928      * @throws  NullPointerException
   929      *          If the <tt>format</tt> is <tt>null</tt>
   930      *
   931      * @return  This writer
   932      *
   933      * @since  1.5
   934      */
   935 //    public PrintWriter format(Locale l, String format, Object ... args) {
   936 //        return format(format, args);
   937 //    }
   938 
   939     /**
   940      * Appends the specified character sequence to this writer.
   941      *
   942      * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
   943      * behaves in exactly the same way as the invocation
   944      *
   945      * <pre>
   946      *     out.write(csq.toString()) </pre>
   947      *
   948      * <p> Depending on the specification of <tt>toString</tt> for the
   949      * character sequence <tt>csq</tt>, the entire sequence may not be
   950      * appended. For instance, invoking the <tt>toString</tt> method of a
   951      * character buffer will return a subsequence whose content depends upon
   952      * the buffer's position and limit.
   953      *
   954      * @param  csq
   955      *         The character sequence to append.  If <tt>csq</tt> is
   956      *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
   957      *         appended to this writer.
   958      *
   959      * @return  This writer
   960      *
   961      * @since  1.5
   962      */
   963     public PrintWriter append(CharSequence csq) {
   964         if (csq == null)
   965             write("null");
   966         else
   967             write(csq.toString());
   968         return this;
   969     }
   970 
   971     /**
   972      * Appends a subsequence of the specified character sequence to this writer.
   973      *
   974      * <p> An invocation of this method of the form <tt>out.append(csq, start,
   975      * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
   976      * exactly the same way as the invocation
   977      *
   978      * <pre>
   979      *     out.write(csq.subSequence(start, end).toString()) </pre>
   980      *
   981      * @param  csq
   982      *         The character sequence from which a subsequence will be
   983      *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
   984      *         will be appended as if <tt>csq</tt> contained the four
   985      *         characters <tt>"null"</tt>.
   986      *
   987      * @param  start
   988      *         The index of the first character in the subsequence
   989      *
   990      * @param  end
   991      *         The index of the character following the last character in the
   992      *         subsequence
   993      *
   994      * @return  This writer
   995      *
   996      * @throws  IndexOutOfBoundsException
   997      *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
   998      *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
   999      *          <tt>csq.length()</tt>
  1000      *
  1001      * @since  1.5
  1002      */
  1003     public PrintWriter append(CharSequence csq, int start, int end) {
  1004         CharSequence cs = (csq == null ? "null" : csq);
  1005         write(cs.subSequence(start, end).toString());
  1006         return this;
  1007     }
  1008 
  1009     /**
  1010      * Appends the specified character to this writer.
  1011      *
  1012      * <p> An invocation of this method of the form <tt>out.append(c)</tt>
  1013      * behaves in exactly the same way as the invocation
  1014      *
  1015      * <pre>
  1016      *     out.write(c) </pre>
  1017      *
  1018      * @param  c
  1019      *         The 16-bit character to append
  1020      *
  1021      * @return  This writer
  1022      *
  1023      * @since 1.5
  1024      */
  1025     public PrintWriter append(char c) {
  1026         write(c);
  1027         return this;
  1028     }
  1029 }