src/share/classes/java/io/PrintWriter.java

Print this page

        

*** 23,34 **** --- 23,37 ---- * questions. */ 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 * class implements all of the <tt>print</tt> methods found in {@link * PrintStream}. It does not contain methods for writing raw bytes, for which
*** 57,78 **** * * @since 1.2 */ protected Writer out; ! private boolean autoFlush = false; private boolean trouble = false; private Formatter formatter; private PrintStream psOut = null; /** * Line separator string. This is the value of the line.separator * property at the moment that the stream was created. */ ! private String lineSeparator; /** * Creates a new PrintWriter, without automatic line flushing. * * @param out A character-output stream */ public PrintWriter (Writer out) { --- 60,98 ---- * * @since 1.2 */ protected Writer out; ! private final boolean autoFlush; private boolean trouble = false; private Formatter formatter; private PrintStream psOut = null; /** * Line separator string. This is the value of the line.separator * property at the moment that the stream was created. */ ! private final String lineSeparator; /** + * Returns a charset object for the given charset name. + * @throws NullPointerException is csn is null + * @throws UnsupportedEncodingException if the charset is not supported + */ + private static Charset toCharset(String csn) + throws UnsupportedEncodingException { + Objects.nonNull(csn, "charsetName"); + try { + return Charset.forName(csn); + } 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 */ public PrintWriter (Writer out) {
*** 162,171 **** --- 182,197 ---- public PrintWriter(String fileName) throws FileNotFoundException { this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))), false); } + /* Private constructor*/ + private PrintWriter(Charset charset, File file) throws FileNotFoundException { + this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)), + false); + } + /** * Creates a new PrintWriter, without automatic line flushing, with the * specified file name and charset. This convenience constructor creates * the necessary intermediate {@link java.io.OutputStreamWriter * OutputStreamWriter}, which will encode characters using the provided
*** 198,209 **** * @since 1.5 */ public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { ! this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)), ! false); } /** * Creates a new PrintWriter, without automatic line flushing, with the * specified file. This convenience constructor creates the necessary --- 224,234 ---- * @since 1.5 */ public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException { ! this(toCharset(csn), new File(fileName)); } /** * Creates a new PrintWriter, without automatic line flushing, with the * specified file. This convenience constructor creates the necessary
*** 270,281 **** * @since 1.5 */ public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException { ! this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)), ! false); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (out == null) --- 295,305 ---- * @since 1.5 */ public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException { ! this(toCharset(csn), file); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (out == null)