--- old/src/share/classes/java/io/PrintWriter.java Thu Dec 16 12:05:26 2010 +++ new/src/share/classes/java/io/PrintWriter.java Thu Dec 16 12:05:25 2010 @@ -25,8 +25,11 @@ package java.io; +import java.util.Objects; import java.util.Formatter; import java.util.Locale; +import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; /** * Prints formatted representations of objects to a text-output stream. This @@ -59,7 +62,7 @@ */ protected Writer out; - private boolean autoFlush = false; + private final boolean autoFlush; private boolean trouble = false; private Formatter formatter; private PrintStream psOut = null; @@ -68,9 +71,27 @@ * Line separator string. This is the value of the line.separator * property at the moment that the stream was created. */ - private String lineSeparator; + private final String lineSeparator; /** + * Verifies that the given charset is supported. + * @throws NullPointerException is csn is null + * @throws UnsupportedEncodingException if the charset is not supported + */ + private static Void verifyCharsetName(String csn) + throws UnsupportedEncodingException { + Objects.nonNull(csn, "charsetName"); + try { + if (Charset.isSupported(csn)) + return null; + } catch (IllegalCharsetNameException unused) { + /* swallow this exception since UnsupportedEncodingException + * will be thrown */ + } + throw new UnsupportedEncodingException(csn); + } + + /** * Creates a new PrintWriter, without automatic line flushing. * * @param out A character-output stream @@ -200,8 +221,8 @@ public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { - this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)), - false); + this(verifyCharsetName(csn), + new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn))); } /** @@ -272,10 +293,20 @@ public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException { - this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)), - false); + this(verifyCharsetName(csn), + new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn))); } + /** + * Private constructor used by public constructors that accept a + * file/filename along with a charset name. This constructor is + * necessary so that the charset name can be verified before + * creating the FileOutputStream. + */ + private PrintWriter(Void unused, Writer writer) { + this(writer, false); + } + /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (out == null)