Skip to content

Commit

Permalink
Fixing handling of Console input.
Browse files Browse the repository at this point in the history
  • Loading branch information
lahodaj committed Oct 25, 2024
1 parent 2936aa8 commit 2c837fd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public char readUserInputChar() throws IOException {
}

public String readUserLine(String prompt) throws IOException {
userOutput().write(prompt);
userOutput().flush();
throw new UserInterruptException("");
}

Expand All @@ -72,6 +74,8 @@ public Writer userOutput() {
}

public char[] readPassword(String prompt) throws IOException {
userOutput().write(prompt);
userOutput().flush();
throw new UserInterruptException("");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4112,6 +4112,8 @@ public void close() throws IOException {
public String readLine(String prompt) {
try {
return input.readUserLine(prompt);
} catch (UserInterruptException ex) {
return null;
} catch (IOException ex) {
throw new IOError(ex);
}
Expand All @@ -4121,6 +4123,8 @@ public String readLine(String prompt) {
public char[] readPassword(String prompt) {
try {
return input.readPassword(prompt);
} catch (UserInterruptException ex) {
return null;
} catch (IOException ex) {
throw new IOError(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ private int readChars(char[] data, int off, int len) throws IOException {

private char[] readChars() throws IOException {
int actualLen = readInt();
if (actualLen == (-1)) {
return null;
}
char[] result = new char[actualLen];
for (int i = 0; i < actualLen; i++) {
result[i] = (char) ((remoteOutput.read() << 8) |
Expand Down Expand Up @@ -257,6 +260,9 @@ public String readLine(Locale locale, String format, Object... args) {
remoteInput.write(Task.READ_LINE.ordinal());
sendChars(chars, 0, chars.length);
char[] line = readChars();
if (line == null) {
return null;
}
return new String(line);
});
} catch (IOException ex) {
Expand Down Expand Up @@ -399,16 +405,24 @@ public synchronized void write(int b) throws IOException {
char[] data = readCharsOrNull(1);
if (data != null) {
String line = console.readLine(new String(data));
char[] chars = line.toCharArray();
sendChars(sinkOutput, chars, 0, chars.length);
if (line == null) {
sendInt(sinkOutput, -1);
} else {
char[] chars = line.toCharArray();
sendChars(sinkOutput, chars, 0, chars.length);
}
bp = 0;
}
}
case READ_PASSWORD -> {
char[] data = readCharsOrNull(1);
if (data != null) {
char[] chars = console.readPassword(new String(data));
sendChars(sinkOutput, chars, 0, chars.length);
if (chars == null) {
sendInt(sinkOutput, -1);
} else {
sendChars(sinkOutput, chars, 0, chars.length);
}
bp = 0;
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/langtools/jdk/jshell/StartOptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ protected void startExCoUoCeCn(Consumer<Integer> checkExitCode,
protected void startCheckUserOutput(Consumer<String> checkUserOutput,
String... args) {
runShell(args);
System.err.println("userout: " + userout);
System.err.println("usererr: " + usererr);
System.err.println("cmdout: " + cmdout);
System.err.println("cmderr: " + cmderr);
check(userout, checkUserOutput, "userout");
check(usererr, null, "usererr");
}
Expand Down Expand Up @@ -390,6 +394,26 @@ public void testPreviewEnabled() {
startCheckUserOutput(s -> assertEquals(s, "prefix\ntest\nsuffix\n"),
"--enable-preview", fn2);
}
public void testInput() {
//readLine(String):
String readLinePrompt = writeToFile(
"""
var v = System.console().readLine("prompt: ");
System.out.println(v);
/exit
""");
startCheckUserOutput(s -> assertEquals(s, "prompt: null\n"),
readLinePrompt);
//readPassword(String):
String readPasswordPrompt = writeToFile(
"""
var v = System.console().readPassword("prompt: ");
System.out.println(java.util.Arrays.toString(v));
/exit
""");
startCheckUserOutput(s -> assertEquals(s, "prompt: null\n"),
readPasswordPrompt);
}

@AfterMethod
public void tearDown() {
Expand Down

0 comments on commit 2c837fd

Please sign in to comment.