8295803: Console should be usable in jshell and other environments

Reviewed-by: jlaskey, alanb
This commit is contained in:
Naoto Sato 2022-12-07 20:49:29 +00:00
parent 5d4c71c8bd
commit 8a9911ef17
17 changed files with 673 additions and 23 deletions

View file

@ -29,6 +29,7 @@ import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.util.Arrays;
import jdk.internal.access.SharedSecrets;
/**
* A utility class for reading passwords
@ -51,13 +52,15 @@ public class Password {
byte[] consoleBytes = null;
try {
// Use the new java.io.Console class
// Only use Console if `in` is the initial System.in
Console con;
if (!isEchoOn && in == System.in && ((con = System.console()) != null)) {
if (!isEchoOn &&
in == SharedSecrets.getJavaLangAccess().initialSystemIn() &&
((con = System.console()) != null)) {
consoleEntered = con.readPassword();
// readPassword returns "" if you just print ENTER,
// readPassword returns "" if you just press ENTER with the built-in Console,
// to be compatible with old Password class, change to null
if (consoleEntered != null && consoleEntered.length == 0) {
if (consoleEntered == null || consoleEntered.length == 0) {
return null;
}
consoleBytes = convertToBytes(consoleEntered);