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

@ -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());
}
/**