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
@ -34,7 +34,7 @@ import jdk.internal.access.SharedSecrets;
|
|||||||
import jdk.internal.io.JdkConsoleImpl;
|
import jdk.internal.io.JdkConsoleImpl;
|
||||||
import jdk.internal.io.JdkConsoleProvider;
|
import jdk.internal.io.JdkConsoleProvider;
|
||||||
import jdk.internal.javac.PreviewFeature;
|
import jdk.internal.javac.PreviewFeature;
|
||||||
import jdk.internal.util.StaticProperty;
|
import sun.nio.cs.UTF_8;
|
||||||
import sun.security.action.GetPropertyAction;
|
import sun.security.action.GetPropertyAction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -580,7 +580,8 @@ public sealed class Console implements Flushable permits ProxyingConsole {
|
|||||||
* the {@code Console}.
|
* the {@code Console}.
|
||||||
* <p>
|
* <p>
|
||||||
* The returned charset corresponds to the input and output source
|
* 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
|
* It may not necessarily be the same as the default charset returned from
|
||||||
* {@link java.nio.charset.Charset#defaultCharset() Charset.defaultCharset()}.
|
* {@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");
|
"Console class itself does not provide implementation");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static native String encoding();
|
|
||||||
private static final boolean istty = istty();
|
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 {
|
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
|
// Set up JavaIOAccess in SharedSecrets
|
||||||
SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
|
SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
|
||||||
public Console console() {
|
public Console console() {
|
||||||
@ -689,6 +671,5 @@ public sealed class Console implements Flushable permits ProxyingConsole {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Console cons;
|
|
||||||
private static native boolean istty();
|
private static native boolean istty();
|
||||||
}
|
}
|
||||||
|
@ -147,8 +147,7 @@ public final class System {
|
|||||||
* corresponds to display output or another output destination
|
* corresponds to display output or another output destination
|
||||||
* specified by the host environment or user. The encoding used
|
* specified by the host environment or user. The encoding used
|
||||||
* in the conversion from characters to bytes is equivalent to
|
* in the conversion from characters to bytes is equivalent to
|
||||||
* {@link Console#charset()} if the {@code Console} exists,
|
* {@link ##stdout.encoding stdout.encoding}.
|
||||||
* <a href="#stdout.encoding">stdout.encoding</a> otherwise.
|
|
||||||
* <p>
|
* <p>
|
||||||
* For simple stand-alone Java applications, a typical way to write
|
* For simple stand-alone Java applications, a typical way to write
|
||||||
* a line of output data is:
|
* 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(long)
|
||||||
* @see java.io.PrintStream#println(java.lang.Object)
|
* @see java.io.PrintStream#println(java.lang.Object)
|
||||||
* @see java.io.PrintStream#println(java.lang.String)
|
* @see java.io.PrintStream#println(java.lang.String)
|
||||||
* @see Console#charset()
|
* @see ##stdout.encoding stdout.encoding
|
||||||
* @see <a href="#stdout.encoding">stdout.encoding</a>
|
|
||||||
*/
|
*/
|
||||||
public static final PrintStream out = null;
|
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
|
* variable {@code out}, has been redirected to a file or other
|
||||||
* destination that is typically not continuously monitored.
|
* destination that is typically not continuously monitored.
|
||||||
* The encoding used in the conversion from characters to bytes is
|
* The encoding used in the conversion from characters to bytes is
|
||||||
* equivalent to {@link Console#charset()} if the {@code Console}
|
* equivalent to {@link ##stderr.encoding stderr.encoding}.
|
||||||
* exists, <a href="#stderr.encoding">stderr.encoding</a> otherwise.
|
|
||||||
*
|
*
|
||||||
* @see Console#charset()
|
* @see ##stderr.encoding stderr.encoding
|
||||||
* @see <a href="#stderr.encoding">stderr.encoding</a>
|
|
||||||
*/
|
*/
|
||||||
public static final PrintStream err = null;
|
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
|
* <td>Character encoding name derived from the host environment and/or
|
||||||
* the user's settings. Setting this system property has no effect.</td></tr>
|
* the user's settings. Setting this system property has no effect.</td></tr>
|
||||||
* <tr><th scope="row">{@systemProperty stdout.encoding}</th>
|
* <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},
|
* 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.
|
* starting it with the property set to another value leads to undefined behavior.
|
||||||
* <tr><th scope="row">{@systemProperty stderr.encoding}</th>
|
* <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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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));
|
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.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* 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;
|
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);
|
|
||||||
}
|
|
||||||
|
53
test/jdk/java/io/Console/DefaultCharsetTest.java
Normal file
53
test/jdk/java/io/Console/DefaultCharsetTest.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 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
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @bug 8341975
|
||||||
|
* @summary Tests the default charset. It should honor `stdout.encoding`
|
||||||
|
* which should be the same as System.out.charset()
|
||||||
|
* @modules jdk.internal.le
|
||||||
|
* @run junit/othervm -Dstdout.encoding=UTF-8 DefaultCharsetTest
|
||||||
|
* @run junit/othervm -Dstdout.encoding=ISO-8859-1 DefaultCharsetTest
|
||||||
|
* @run junit/othervm -Dstdout.encoding=US-ASCII DefaultCharsetTest
|
||||||
|
* @run junit/othervm -Dstdout.encoding=foo DefaultCharsetTest
|
||||||
|
* @run junit/othervm DefaultCharsetTest
|
||||||
|
*/
|
||||||
|
public class DefaultCharsetTest {
|
||||||
|
@Test
|
||||||
|
public void testDefaultCharset() {
|
||||||
|
var stdoutEncoding = System.getProperty("stdout.encoding");
|
||||||
|
var sysoutCharset = System.out.charset();
|
||||||
|
var consoleCharset = System.console().charset();
|
||||||
|
System.out.println("""
|
||||||
|
stdout.encoding = %s
|
||||||
|
System.out.charset() = %s
|
||||||
|
System.console().charset() = %s
|
||||||
|
""".formatted(stdoutEncoding, sysoutCharset.name(), consoleCharset.name()));
|
||||||
|
assertEquals(consoleCharset, sysoutCharset,
|
||||||
|
"Charsets for System.out and Console differ for stdout.encoding: %s".formatted(stdoutEncoding));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user