8342936: Enhance java.io.IO with parameter-less println() and readln()

Reviewed-by: asotona, jpai, naoto
This commit is contained in:
Jan Lahoda 2024-11-14 08:22:51 +00:00
parent b54bd824b5
commit c3776db498
14 changed files with 252 additions and 10 deletions

View file

@ -172,6 +172,19 @@ public sealed class Console implements Flushable permits ProxyingConsole {
throw newUnsupportedOperationException();
}
/**
* Terminates the current line in this console's output stream using
* {@link System#lineSeparator()} and then flushes the console.
*
* @return This console
*
* @since 24
*/
@PreviewFeature(feature = PreviewFeature.Feature.IMPLICIT_CLASSES)
public Console println() {
return println("");
}
/**
* Writes a string representation of the specified object to this console's
* output stream and then flushes the console.
@ -214,6 +227,24 @@ public sealed class Console implements Flushable permits ProxyingConsole {
throw newUnsupportedOperationException();
}
/**
* Reads a single line of text from this console.
*
* @throws IOError
* If an I/O error occurs.
*
* @return A string containing the line read from the console, not
* including any line-termination characters, or {@code null}
* if an end of stream has been reached without having read
* any characters.
*
* @since 24
*/
@PreviewFeature(feature = PreviewFeature.Feature.IMPLICIT_CLASSES)
public String readln() {
throw newUnsupportedOperationException();
}
/**
* Writes a formatted string to this console's output stream using
* the specified format string and arguments with the

View file

@ -63,6 +63,21 @@ public final class IO {
con().println(obj);
}
/**
* Terminates the current line on the system console and then flushes
* that console.
*
* <p> The effect is as if {@link Console#println() println()}
* had been called on {@code System.console()}.
*
* @throws IOError if {@code System.console()} returns {@code null},
* or if an I/O error occurs
* @since 24
*/
public static void println() {
con().println();
}
/**
* Writes a string representation of the specified object to the system
* console and then flushes that console.
@ -99,6 +114,24 @@ public final class IO {
return con().readln(prompt);
}
/**
* Reads a single line of text from the system console.
*
* <p> The effect is as if {@link Console#readln() readln()}
* had been called on {@code System.console()}.
*
* @return a string containing the line read from the system console, not
* including any line-termination characters. Returns {@code null} if an
* end of stream has been reached without having read any characters.
*
* @throws IOError if {@code System.console()} returns {@code null},
* or if an I/O error occurs
* @since 24
*/
public static String readln() {
return con().readln();
}
private static Console con() {
var con = System.console();
if (con != null) {

View file

@ -117,6 +117,18 @@ final class ProxyingConsole extends Console {
}
}
/**
* {@inheritDoc}
*
* @throws IOError {@inheritDoc}
*/
@Override
public String readln() {
synchronized (readLock) {
return delegate.readln();
}
}
/**
* {@inheritDoc}
*/