KT-11208 Remove readLine's new charset parameter
It can't be introduced in a patch release.
This commit is contained in:
@@ -139,22 +139,14 @@ public inline fun println() {
|
|||||||
private const val BUFFER_SIZE: Int = 32
|
private const val BUFFER_SIZE: Int = 32
|
||||||
private const val LINE_SEPARATOR_MAX_LENGTH: Int = 2
|
private const val LINE_SEPARATOR_MAX_LENGTH: Int = 2
|
||||||
|
|
||||||
private var cachedDecoder: CharsetDecoder? = null
|
private val decoder: CharsetDecoder by lazy { Charset.defaultCharset().newDecoder() }
|
||||||
|
|
||||||
private fun decoderFor(charset: Charset): CharsetDecoder {
|
|
||||||
return cachedDecoder?.takeIf { cached ->
|
|
||||||
cached.charset() == charset
|
|
||||||
} ?: charset.newDecoder().also { newDecoder ->
|
|
||||||
cachedDecoder = newDecoder
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a line of input from the standard input stream.
|
* Reads a line of input from the standard input stream.
|
||||||
*
|
*
|
||||||
* @return the line read or `null` if the input stream is redirected to a file and the end of file has been reached.
|
* @return the line read or `null` if the input stream is redirected to a file and the end of file has been reached.
|
||||||
*/
|
*/
|
||||||
fun readLine(charset: Charset = Charset.defaultCharset()): String? = readLine(System.`in`, decoderFor(charset))
|
fun readLine(): String? = readLine(System.`in`, decoder)
|
||||||
|
|
||||||
internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String? {
|
internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String? {
|
||||||
require(decoder.maxCharsPerByte() <= 1) { "Encodings with multiple chars per byte are not supported" }
|
require(decoder.maxCharsPerByte() <= 1) { "Encodings with multiple chars per byte are not supported" }
|
||||||
@@ -165,7 +157,7 @@ internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String
|
|||||||
|
|
||||||
var read = inputStream.read()
|
var read = inputStream.read()
|
||||||
if (read == -1) return null
|
if (read == -1) return null
|
||||||
while (read != -1) {
|
do {
|
||||||
byteBuffer.put(read.toByte())
|
byteBuffer.put(read.toByte())
|
||||||
if (decoder.tryDecode(byteBuffer, charBuffer, false)) {
|
if (decoder.tryDecode(byteBuffer, charBuffer, false)) {
|
||||||
if (charBuffer.containsLineSeparator()) {
|
if (charBuffer.containsLineSeparator()) {
|
||||||
@@ -176,7 +168,7 @@ internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
read = inputStream.read()
|
read = inputStream.read()
|
||||||
}
|
} while (read != -1)
|
||||||
|
|
||||||
with(decoder) {
|
with(decoder) {
|
||||||
tryDecode(byteBuffer, charBuffer, true) // throws exception if undecoded bytes are left
|
tryDecode(byteBuffer, charBuffer, true) // throws exception if undecoded bytes are left
|
||||||
|
|||||||
Reference in New Issue
Block a user