Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/java/io/PrintWriter.java
          +++ new/src/share/classes/java/io/PrintWriter.java
↓ open down ↓ 17 lines elided ↑ open up ↑
  18   18   * 2 along with this work; if not, write to the Free Software Foundation,
  19   19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20   20   *
  21   21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22   22   * or visit www.oracle.com if you need additional information or have any
  23   23   * questions.
  24   24   */
  25   25  
  26   26  package java.io;
  27   27  
       28 +import java.util.Objects;
  28   29  import java.util.Formatter;
  29   30  import java.util.Locale;
       31 +import java.nio.charset.Charset;
       32 +import java.nio.charset.IllegalCharsetNameException;
  30   33  
  31   34  /**
  32   35   * Prints formatted representations of objects to a text-output stream.  This
  33   36   * class implements all of the <tt>print</tt> methods found in {@link
  34   37   * PrintStream}.  It does not contain methods for writing raw bytes, for which
  35   38   * a program should use unencoded byte streams.
  36   39   *
  37   40   * <p> Unlike the {@link PrintStream} class, if automatic flushing is enabled
  38   41   * it will be done only when one of the <tt>println</tt>, <tt>printf</tt>, or
  39   42   * <tt>format</tt> methods is invoked, rather than whenever a newline character
↓ open down ↓ 12 lines elided ↑ open up ↑
  52   55  public class PrintWriter extends Writer {
  53   56  
  54   57      /**
  55   58       * The underlying character-output stream of this
  56   59       * <code>PrintWriter</code>.
  57   60       *
  58   61       * @since 1.2
  59   62       */
  60   63      protected Writer out;
  61   64  
  62      -    private boolean autoFlush = false;
       65 +    private final boolean autoFlush;
  63   66      private boolean trouble = false;
  64   67      private Formatter formatter;
  65   68      private PrintStream psOut = null;
  66   69  
  67   70      /**
  68   71       * Line separator string.  This is the value of the line.separator
  69   72       * property at the moment that the stream was created.
  70   73       */
  71      -    private String lineSeparator;
       74 +    private final String lineSeparator;
  72   75  
  73   76      /**
       77 +     * Returns a charset object for the given charset name.
       78 +     * @throws NullPointerException          is csn is null
       79 +     * @throws UnsupportedEncodingException  if the charset is not supported
       80 +     */
       81 +    private static Charset toCharset(String csn)
       82 +            throws UnsupportedEncodingException {
       83 +        Objects.nonNull(csn, "charsetName");
       84 +        try {
       85 +            return Charset.forName(csn);
       86 +        } catch (IllegalCharsetNameException unused) {
       87 +            /* swallow this exception since UnsupportedEncodingException
       88 +             * will be thrown */
       89 +        }
       90 +        throw new UnsupportedEncodingException(csn);
       91 +    }
       92 +
       93 +    /**
  74   94       * Creates a new PrintWriter, without automatic line flushing.
  75   95       *
  76   96       * @param  out        A character-output stream
  77   97       */
  78   98      public PrintWriter (Writer out) {
  79   99          this(out, false);
  80  100      }
  81  101  
  82  102      /**
  83  103       * Creates a new PrintWriter.
↓ open down ↓ 73 lines elided ↑ open up ↑
 157  177       *          SecurityManager#checkWrite checkWrite(fileName)} denies write
 158  178       *          access to the file
 159  179       *
 160  180       * @since  1.5
 161  181       */
 162  182      public PrintWriter(String fileName) throws FileNotFoundException {
 163  183          this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
 164  184               false);
 165  185      }
 166  186  
      187 +    /* Private constructor*/
      188 +    private PrintWriter(Charset charset, File file) throws FileNotFoundException {
      189 +        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
      190 +             false);
      191 +    }
      192 +
 167  193      /**
 168  194       * Creates a new PrintWriter, without automatic line flushing, with the
 169  195       * specified file name and charset.  This convenience constructor creates
 170  196       * the necessary intermediate {@link java.io.OutputStreamWriter
 171  197       * OutputStreamWriter}, which will encode characters using the provided
 172  198       * charset.
 173  199       *
 174  200       * @param  fileName
 175  201       *         The name of the file to use as the destination of this writer.
 176  202       *         If the file exists then it will be truncated to zero size;
↓ open down ↓ 16 lines elided ↑ open up ↑
 193  219       *          access to the file
 194  220       *
 195  221       * @throws  UnsupportedEncodingException
 196  222       *          If the named charset is not supported
 197  223       *
 198  224       * @since  1.5
 199  225       */
 200  226      public PrintWriter(String fileName, String csn)
 201  227          throws FileNotFoundException, UnsupportedEncodingException
 202  228      {
 203      -        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)),
 204      -             false);
      229 +        this(toCharset(csn), new File(fileName));
 205  230      }
 206  231  
 207  232      /**
 208  233       * Creates a new PrintWriter, without automatic line flushing, with the
 209  234       * specified file.  This convenience constructor creates the necessary
 210  235       * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
 211  236       * which will encode characters using the {@linkplain
 212  237       * java.nio.charset.Charset#defaultCharset() default charset} for this
 213  238       * instance of the Java virtual machine.
 214  239       *
↓ open down ↓ 50 lines elided ↑ open up ↑
 265  290       *          denies write access to the file
 266  291       *
 267  292       * @throws  UnsupportedEncodingException
 268  293       *          If the named charset is not supported
 269  294       *
 270  295       * @since  1.5
 271  296       */
 272  297      public PrintWriter(File file, String csn)
 273  298          throws FileNotFoundException, UnsupportedEncodingException
 274  299      {
 275      -        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)),
 276      -             false);
      300 +        this(toCharset(csn), file);
 277  301      }
 278  302  
 279  303      /** Checks to make sure that the stream has not been closed */
 280  304      private void ensureOpen() throws IOException {
 281  305          if (out == null)
 282  306              throw new IOException("Stream closed");
 283  307      }
 284  308  
 285  309      /**
 286  310       * Flushes the stream.
↓ open down ↓ 753 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX