Skip to content
Permalink
Browse files
CODETOOLS-7902915: JMH: Use Console.charset() if available
  • Loading branch information
shipilev committed May 4, 2021
1 parent 5e497a9 commit 03fa35a8283be10c5db52a4f25eeaef8286a0176
Showing with 22 additions and 10 deletions.
  1. +22 −10 jmh-core/src/main/java/org/openjdk/jmh/util/Utils.java
@@ -238,22 +238,34 @@ public static Charset guessConsoleEncoding() {
// We cannot use Console class directly, because we also need the access to the raw byte stream,
// e.g. for pushing in a raw output from a forked VM invocation. Therefore, we are left with
// reflectively poking out the Charset from Console, and use it for our own private output streams.
// Since JDK 17, there is Console.charset(), which we can use reflectively.

// Try 1. Try to poke the System.console().
try {
Console console = System.console();
if (console != null) {
Field f = Console.class.getDeclaredField("cs");
setAccessible(console, f);
Object res = f.get(console);
if (res instanceof Charset) {
return (Charset) res;
{
Method m = Console.class.getDeclaredMethod("charset");
Object res = m.invoke(console);
if (res instanceof Charset) {
return (Charset) res;
}
}
{
Field f = Console.class.getDeclaredField("cs");
setAccessible(console, f);
Object res = f.get(console);
if (res instanceof Charset) {
return (Charset) res;
}
}
Method m = Console.class.getDeclaredMethod("encoding");
setAccessible(console, m);
res = m.invoke(null);
if (res instanceof String) {
return Charset.forName((String) res);
{
Method m = Console.class.getDeclaredMethod("encoding");
setAccessible(console, m);
Object res = m.invoke(null);
if (res instanceof String) {
return Charset.forName((String) res);
}
}
}
} catch (Exception e) {

0 comments on commit 03fa35a

Please sign in to comment.