8341975: Unable to set encoding for IO.println, IO.print and IO.readln

Reviewed-by: rriggs, iris, alanb
This commit is contained in:
Naoto Sato 2024-10-23 17:44:15 +00:00
parent a522d216b5
commit 426da4bbad
5 changed files with 67 additions and 56 deletions

View file

@ -34,7 +34,7 @@ import jdk.internal.access.SharedSecrets;
import jdk.internal.io.JdkConsoleImpl;
import jdk.internal.io.JdkConsoleProvider;
import jdk.internal.javac.PreviewFeature;
import jdk.internal.util.StaticProperty;
import sun.nio.cs.UTF_8;
import sun.security.action.GetPropertyAction;
/**
@ -580,7 +580,8 @@ public sealed class Console implements Flushable permits ProxyingConsole {
* the {@code Console}.
* <p>
* The returned charset corresponds to the input and output source
* (e.g., keyboard and/or display) specified by the host environment or user.
* (e.g., keyboard and/or display) specified by the host environment or user,
* which defaults to the one based on {@link System##stdout.encoding stdout.encoding}.
* It may not necessarily be the same as the default charset returned from
* {@link java.nio.charset.Charset#defaultCharset() Charset.defaultCharset()}.
*
@ -614,30 +615,11 @@ public sealed class Console implements Flushable permits ProxyingConsole {
"Console class itself does not provide implementation");
}
private static native String encoding();
private static final boolean istty = istty();
static final Charset CHARSET;
static final Charset CHARSET =
Charset.forName(GetPropertyAction.privilegedGetProperty("stdout.encoding"), UTF_8.INSTANCE);
private static final Console cons = instantiateConsole();
static {
Charset cs = null;
if (istty) {
String csname = encoding();
if (csname == null) {
csname = GetPropertyAction.privilegedGetProperty("stdout.encoding");
}
if (csname != null) {
cs = Charset.forName(csname, null);
}
}
if (cs == null) {
cs = Charset.forName(StaticProperty.nativeEncoding(),
Charset.defaultCharset());
}
CHARSET = cs;
cons = instantiateConsole();
// Set up JavaIOAccess in SharedSecrets
SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
public Console console() {
@ -689,6 +671,5 @@ public sealed class Console implements Flushable permits ProxyingConsole {
return c;
}
private static final Console cons;
private static native boolean istty();
}

View file

@ -147,8 +147,7 @@ public final class System {
* corresponds to display output or another output destination
* specified by the host environment or user. The encoding used
* in the conversion from characters to bytes is equivalent to
* {@link Console#charset()} if the {@code Console} exists,
* <a href="#stdout.encoding">stdout.encoding</a> otherwise.
* {@link ##stdout.encoding stdout.encoding}.
* <p>
* For simple stand-alone Java applications, a typical way to write
* a line of output data is:
@ -168,8 +167,7 @@ public final class System {
* @see java.io.PrintStream#println(long)
* @see java.io.PrintStream#println(java.lang.Object)
* @see java.io.PrintStream#println(java.lang.String)
* @see Console#charset()
* @see <a href="#stdout.encoding">stdout.encoding</a>
* @see ##stdout.encoding stdout.encoding
*/
public static final PrintStream out = null;
@ -185,11 +183,9 @@ public final class System {
* variable {@code out}, has been redirected to a file or other
* destination that is typically not continuously monitored.
* The encoding used in the conversion from characters to bytes is
* equivalent to {@link Console#charset()} if the {@code Console}
* exists, <a href="#stderr.encoding">stderr.encoding</a> otherwise.
* equivalent to {@link ##stderr.encoding stderr.encoding}.
*
* @see Console#charset()
* @see <a href="#stderr.encoding">stderr.encoding</a>
* @see ##stderr.encoding stderr.encoding
*/
public static final PrintStream err = null;
@ -788,7 +784,8 @@ public final class System {
* <td>Character encoding name derived from the host environment and/or
* the user's settings. Setting this system property has no effect.</td></tr>
* <tr><th scope="row">{@systemProperty stdout.encoding}</th>
* <td>Character encoding name for {@link System#out System.out}.
* <td>Character encoding name for {@link System#out System.out} and
* {@link System#console() System.console()}.
* The Java runtime can be started with the system property set to {@code UTF-8},
* starting it with the property set to another value leads to undefined behavior.
* <tr><th scope="row">{@systemProperty stderr.encoding}</th>