From 91aa956f3452f8c241ce9c0642951541fcc89538 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Thu, 2 Sep 2021 01:18:16 +0300 Subject: [PATCH] Introduce JVM readln() and readlnOrNull() top-level functions #KT-48456 --- libraries/stdlib/jvm/src/kotlin/io/Console.kt | 24 +++++++++++++++++++ .../kotlin-stdlib-runtime-merged.txt | 2 ++ 2 files changed, 26 insertions(+) diff --git a/libraries/stdlib/jvm/src/kotlin/io/Console.kt b/libraries/stdlib/jvm/src/kotlin/io/Console.kt index 987bba23499..eba084e5137 100644 --- a/libraries/stdlib/jvm/src/kotlin/io/Console.kt +++ b/libraries/stdlib/jvm/src/kotlin/io/Console.kt @@ -140,6 +140,30 @@ public actual inline fun println() { System.out.println() } +/** + * Reads a line of input from the standard input stream and returns it, + * or throws a [RuntimeException] if EOF has already been reached when [readln] is called. + * + * LF or CRLF is treated as the line terminator. Line terminator is not included in the returned string. + * + * The input is decoded using the system default Charset. A [CharacterCodingException] is thrown if input is malformed. + */ +@SinceKotlin("1.6") +public fun readln(): String = readlnOrNull() ?: throw ReadAfterEOFException("EOF has already been reached") + +/** + * Reads a line of input from the standard input stream and returns it, + * or return `null` if EOF has already been reached when [readlnOrNull] is called. + * + * LF or CRLF is treated as the line terminator. Line terminator is not included in the returned string. + * + * The input is decoded using the system default Charset. A [CharacterCodingException] is thrown if input is malformed. + */ +@SinceKotlin("1.6") +public fun readlnOrNull(): String? = readLine() + +private class ReadAfterEOFException(message: String?) : RuntimeException(message) + /** * Reads a line of input from the standard input stream. * diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 407fa7b4f40..d90f64ed5f3 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -3098,6 +3098,8 @@ public final class kotlin/io/CloseableKt { public final class kotlin/io/ConsoleKt { public static final fun readLine ()Ljava/lang/String; + public static final fun readln ()Ljava/lang/String; + public static final fun readlnOrNull ()Ljava/lang/String; } public final class kotlin/io/ConstantsKt {