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