8276970: Default charset for PrintWriter that wraps PrintStream

Reviewed-by: rriggs, alanb
This commit is contained in:
Naoto Sato 2021-11-18 01:12:12 +00:00
parent 29e552c03a
commit 231fb61aae
4 changed files with 133 additions and 9 deletions

View file

@ -99,7 +99,9 @@ public class OutputStreamWriter extends Writer {
}
/**
* Creates an OutputStreamWriter that uses the default character encoding.
* Creates an OutputStreamWriter that uses the default character encoding, or
* where {@code out} is a {@code PrintStream}, the charset used by the print
* stream.
*
* @param out An OutputStream
* @see Charset#defaultCharset()
@ -107,7 +109,7 @@ public class OutputStreamWriter extends Writer {
public OutputStreamWriter(OutputStream out) {
super(out);
se = StreamEncoder.forOutputStreamWriter(out, this,
Charset.defaultCharset());
out instanceof PrintStream ps ? ps.charset() : Charset.defaultCharset());
}
/**

View file

@ -68,6 +68,7 @@ public class PrintStream extends FilterOutputStream
private final boolean autoFlush;
private boolean trouble = false;
private Formatter formatter;
private final Charset charset;
/**
* Track both the text- and character-output streams, so that their buffers
@ -108,7 +109,8 @@ public class PrintStream extends FilterOutputStream
private PrintStream(boolean autoFlush, OutputStream out) {
super(out);
this.autoFlush = autoFlush;
this.charOut = new OutputStreamWriter(this);
this.charset = out instanceof PrintStream ps ? ps.charset() : Charset.defaultCharset();
this.charOut = new OutputStreamWriter(this, charset);
this.textOut = new BufferedWriter(charOut);
}
@ -124,7 +126,8 @@ public class PrintStream extends FilterOutputStream
/**
* Creates a new print stream, without automatic line flushing, with the
* specified OutputStream. Characters written to the stream are converted
* to bytes using the default charset.
* to bytes using the default charset, or where {@code out} is a
* {@code PrintStream}, the charset used by the print stream.
*
* @param out The output stream to which values and objects will be
* printed
@ -139,7 +142,8 @@ public class PrintStream extends FilterOutputStream
/**
* Creates a new print stream, with the specified OutputStream and line
* flushing. Characters written to the stream are converted to bytes using
* the default charset.
* the default charset, or where {@code out} is a {@code PrintStream},
* the charset used by the print stream.
*
* @param out The output stream to which values and objects will be
* printed
@ -201,6 +205,7 @@ public class PrintStream extends FilterOutputStream
this.autoFlush = autoFlush;
this.charOut = new OutputStreamWriter(this, charset);
this.textOut = new BufferedWriter(charOut);
this.charset = charset;
}
/**
@ -1374,4 +1379,12 @@ public class PrintStream extends FilterOutputStream
return this;
}
/**
* {@return the charset used in this {@code PrintStream} instance}
*
* @since 18
*/
public Charset charset() {
return charset;
}
}

View file

@ -118,7 +118,8 @@ public class PrintWriter extends Writer {
* Creates a new PrintWriter, without automatic line flushing, from an
* existing OutputStream. This convenience constructor creates the
* necessary intermediate OutputStreamWriter, which will convert characters
* into bytes using the default charset.
* into bytes using the default charset, or where {@code out} is a
* {@code PrintStream}, the charset used by the print stream.
*
* @param out An output stream
*
@ -132,8 +133,9 @@ public class PrintWriter extends Writer {
/**
* Creates a new PrintWriter from an existing OutputStream. This
* convenience constructor creates the necessary intermediate
* OutputStreamWriter, which will convert characters into bytes using the
* default charset.
* OutputStreamWriter, which will convert characters into bytes using
* the default charset, or where {@code out} is a {@code PrintStream},
* the charset used by the print stream.
*
* @param out An output stream
* @param autoFlush A boolean; if true, the {@code println},
@ -144,7 +146,7 @@ public class PrintWriter extends Writer {
* @see Charset#defaultCharset()
*/
public PrintWriter(OutputStream out, boolean autoFlush) {
this(out, autoFlush, Charset.defaultCharset());
this(out, autoFlush, out instanceof PrintStream ps ? ps.charset() : Charset.defaultCharset());
}
/**