Skip to content

Commit 3ec0cfb

Browse files
committed
initial commit
1 parent 580eb62 commit 3ec0cfb

File tree

4 files changed

+53
-40
lines changed

4 files changed

+53
-40
lines changed

src/java.base/share/classes/java/io/Console.java

+3-18
Original file line numberDiff line numberDiff line change
@@ -614,27 +614,12 @@ private static UnsupportedOperationException newUnsupportedOperationException()
614614
"Console class itself does not provide implementation");
615615
}
616616

617-
private static native String encoding();
618617
private static final boolean istty = istty();
619618
static final Charset CHARSET;
620619
static {
621-
Charset cs = null;
622-
623-
if (istty) {
624-
String csname = encoding();
625-
if (csname == null) {
626-
csname = GetPropertyAction.privilegedGetProperty("stdout.encoding");
627-
}
628-
if (csname != null) {
629-
cs = Charset.forName(csname, null);
630-
}
631-
}
632-
if (cs == null) {
633-
cs = Charset.forName(StaticProperty.nativeEncoding(),
634-
Charset.defaultCharset());
635-
}
636-
637-
CHARSET = cs;
620+
CHARSET = Charset.forName(GetPropertyAction.privilegedGetProperty("stdout.encoding"),
621+
Charset.forName(StaticProperty.nativeEncoding(),
622+
Charset.defaultCharset()));
638623

639624
cons = instantiateConsole();
640625

src/java.base/unix/native/libjava/Console_md.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* 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)
3636
{
3737
return isatty(fileno(stdin)) && isatty(fileno(stdout));
3838
}
39-
40-
JNIEXPORT jstring JNICALL
41-
Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
42-
{
43-
return NULL;
44-
}

src/java.base/windows/native/libjava/Console_md.c

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* 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)
4949

5050
return JNI_TRUE;
5151
}
52-
53-
JNIEXPORT jstring JNICALL
54-
Java_java_io_Console_encoding(JNIEnv *env, jclass cls)
55-
{
56-
char buf[64];
57-
int cp = GetConsoleCP();
58-
if (cp >= 874 && cp <= 950)
59-
snprintf(buf, sizeof(buf), "ms%d", cp);
60-
else if (cp == 65001)
61-
snprintf(buf, sizeof(buf), "UTF-8");
62-
else
63-
snprintf(buf, sizeof(buf), "cp%d", cp);
64-
return JNU_NewStringPlatform(env, buf);
65-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8341975
27+
* @summary Tests the default charset. It should honor `stdout.encoding`
28+
* which should be the same as System.out.charset()
29+
* @run main/othervm -Dstdout.encoding=UTF-8 DefaultCharsetTest UTF-8
30+
* @run main/othervm -Dstdout.encoding=ISO-8859-1 DefaultCharsetTest ISO-8859-1
31+
* @run main/othervm -Dstdout.encoding=US-ASCII DefaultCharsetTest US-ASCII
32+
* @run main/othervm -Dstdout.encoding=foo DefaultCharsetTest foo
33+
* @run main/othervm DefaultCharsetTest (none)
34+
*/
35+
public class DefaultCharsetTest {
36+
public static void main(String... args) {
37+
var sysoutCharset = System.out.charset();
38+
var consoleCharset = System.console().charset();
39+
System.out.println("""
40+
stdout.encoding = %s
41+
System.out.charset() = %s
42+
System.console().charset() = %s
43+
""".formatted(args[0], sysoutCharset.name(), consoleCharset.name()));
44+
if (!sysoutCharset.equals(consoleCharset)) {
45+
throw new RuntimeException("Charsets for System.out and Console differ.");
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)