mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8341975: Unable to set encoding for IO.println, IO.print and IO.readln
Reviewed-by: rriggs, iris, alanb
This commit is contained in:
parent
a522d216b5
commit
426da4bbad
5 changed files with 67 additions and 56 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -36,9 +36,3 @@ Java_java_io_Console_istty(JNIEnv *env, jclass cls)
|
|||
{
|
||||
return isatty(fileno(stdin)) && isatty(fileno(stdout));
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -49,17 +49,3 @@ Java_java_io_Console_istty(JNIEnv *env, jclass cls)
|
|||
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
|
||||
{
|
||||
char buf[64];
|
||||
int cp = GetConsoleCP();
|
||||
if (cp >= 874 && cp <= 950)
|
||||
snprintf(buf, sizeof(buf), "ms%d", cp);
|
||||
else if (cp == 65001)
|
||||
snprintf(buf, sizeof(buf), "UTF-8");
|
||||
else
|
||||
snprintf(buf, sizeof(buf), "cp%d", cp);
|
||||
return JNU_NewStringPlatform(env, buf);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue