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 +     * Verifies that the given charset is supported.
       78 +     * @throws NullPointerException          is csn is null
       79 +     * @throws UnsupportedEncodingException  if the charset is not supported
       80 +     */
       81 +    private static Void verifyCharsetName(String csn)
       82 +            throws UnsupportedEncodingException {
       83 +        Objects.nonNull(csn, "charsetName");
       84 +        try {
       85 +            if (Charset.isSupported(csn))
       86 +                return null;
       87 +        } catch (IllegalCharsetNameException unused) {
       88 +            /* swallow this exception since UnsupportedEncodingException
       89 +             * will be thrown */
       90 +        }
       91 +        throw new UnsupportedEncodingException(csn);
       92 +    }
       93 +
       94 +    /**
  74   95       * Creates a new PrintWriter, without automatic line flushing.
  75   96       *
  76   97       * @param  out        A character-output stream
  77   98       */
  78   99      public PrintWriter (Writer out) {
  79  100          this(out, false);
  80  101      }
  81  102  
  82  103      /**
  83  104       * Creates a new PrintWriter.
↓ open down ↓ 109 lines elided ↑ open up ↑
 193  214       *          access to the file
 194  215       *
 195  216       * @throws  UnsupportedEncodingException
 196  217       *          If the named charset is not supported
 197  218       *
 198  219       * @since  1.5
 199  220       */
 200  221      public PrintWriter(String fileName, String csn)
 201  222          throws FileNotFoundException, UnsupportedEncodingException
 202  223      {
 203      -        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)),
 204      -             false);
      224 +        this(verifyCharsetName(csn),
      225 +             new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)));
 205  226      }
 206  227  
 207  228      /**
 208  229       * Creates a new PrintWriter, without automatic line flushing, with the
 209  230       * specified file.  This convenience constructor creates the necessary
 210  231       * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
 211  232       * which will encode characters using the {@linkplain
 212  233       * java.nio.charset.Charset#defaultCharset() default charset} for this
 213  234       * instance of the Java virtual machine.
 214  235       *
↓ open down ↓ 50 lines elided ↑ open up ↑
 265  286       *          denies write access to the file
 266  287       *
 267  288       * @throws  UnsupportedEncodingException
 268  289       *          If the named charset is not supported
 269  290       *
 270  291       * @since  1.5
 271  292       */
 272  293      public PrintWriter(File file, String csn)
 273  294          throws FileNotFoundException, UnsupportedEncodingException
 274  295      {
 275      -        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)),
 276      -             false);
      296 +        this(verifyCharsetName(csn),
      297 +             new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)));
 277  298      }
 278  299  
      300 +    /**
      301 +     * Private constructor used by public constructors that accept a
      302 +     * file/filename along with a charset name. This constructor is
      303 +     * necessary so that the charset name can be verified before
      304 +     * creating the FileOutputStream.
      305 +     */
      306 +    private PrintWriter(Void unused, Writer writer) {
      307 +        this(writer, false);
      308 +    }
      309 +
 279  310      /** Checks to make sure that the stream has not been closed */
 280  311      private void ensureOpen() throws IOException {
 281  312          if (out == null)
 282  313              throw new IOException("Stream closed");
 283  314      }
 284  315  
 285  316      /**
 286  317       * Flushes the stream.
 287  318       * @see #checkError()
 288  319       */
↓ open down ↓ 751 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX