KT-11208 Remove stream buffering
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
@file:JvmVersion
|
||||
@file:JvmName("ConsoleKt")
|
||||
|
||||
package kotlin.io
|
||||
|
||||
import java.io.InputStream
|
||||
import java.io.InputStreamReader
|
||||
import java.io.BufferedReader
|
||||
import java.nio.Buffer
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.CharBuffer
|
||||
import java.nio.charset.Charset
|
||||
import java.nio.charset.CharsetDecoder
|
||||
|
||||
/** Prints the given message to the standard output stream. */
|
||||
@kotlin.internal.InlineOnly
|
||||
@@ -132,50 +136,89 @@ public inline fun println() {
|
||||
System.out.println()
|
||||
}
|
||||
|
||||
// Since System.in can change its value on the course of program running,
|
||||
// we should always delegate to current value and cannot just pass it to InputStreamReader constructor.
|
||||
// We could use "by" implementation, but we can only use "by" with interfaces and InputStream is abstract class.
|
||||
private val stdin: BufferedReader by lazy { BufferedReader(InputStreamReader(object : InputStream() {
|
||||
public override fun read(): Int {
|
||||
return System.`in`.read()
|
||||
}
|
||||
private const val BUFFER_SIZE: Int = 32
|
||||
|
||||
public override fun reset() {
|
||||
System.`in`.reset()
|
||||
}
|
||||
// Since System.in can change its value on the course of program running, we should always delegate to current value.
|
||||
private val stdin: InputStream
|
||||
get() = System.`in`
|
||||
|
||||
public override fun read(b: ByteArray): Int {
|
||||
return System.`in`.read(b)
|
||||
}
|
||||
private var cachedDecoder: CharsetDecoder? = null
|
||||
|
||||
public override fun close() {
|
||||
System.`in`.close()
|
||||
private fun decoderFor(charset: Charset): CharsetDecoder {
|
||||
return cachedDecoder?.takeIf { cached ->
|
||||
cached.charset() == charset
|
||||
} ?: charset.newDecoder().also { newDecoder ->
|
||||
cachedDecoder = newDecoder
|
||||
}
|
||||
|
||||
public override fun mark(readlimit: Int) {
|
||||
System.`in`.mark(readlimit)
|
||||
}
|
||||
|
||||
public override fun skip(n: Long): Long {
|
||||
return System.`in`.skip(n)
|
||||
}
|
||||
|
||||
public override fun available(): Int {
|
||||
return System.`in`.available()
|
||||
}
|
||||
|
||||
public override fun markSupported(): Boolean {
|
||||
return System.`in`.markSupported()
|
||||
}
|
||||
|
||||
public override fun read(b: ByteArray, off: Int, len: Int): Int {
|
||||
return System.`in`.read(b, off, len)
|
||||
}
|
||||
}))}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public fun readLine(): String? = stdin.readLine()
|
||||
fun readLine(lineSeparator: String = System.lineSeparator(), charset: Charset = Charset.defaultCharset()): String? =
|
||||
readLine(stdin, lineSeparator, decoderFor(charset))
|
||||
|
||||
internal fun readLine(inputStream: InputStream, lineSeparator: String, decoder: CharsetDecoder): String? {
|
||||
require(decoder.maxCharsPerByte() <= 1) { "Encodings with multiple chars per byte are not supported" }
|
||||
|
||||
val byteBuffer = ByteBuffer.allocate(BUFFER_SIZE)
|
||||
val charBuffer = CharBuffer.allocate(lineSeparator.length)
|
||||
val stringBuilder = StringBuilder()
|
||||
|
||||
var read = inputStream.read()
|
||||
if (read == -1) return null
|
||||
while (read != -1) {
|
||||
byteBuffer.put(read.toByte())
|
||||
if (decoder.tryDecode(byteBuffer, charBuffer, false)) {
|
||||
if (charBuffer.contentEquals(lineSeparator)) {
|
||||
break
|
||||
}
|
||||
if (!charBuffer.hasRemaining()) {
|
||||
stringBuilder.append(charBuffer.dequeue())
|
||||
}
|
||||
}
|
||||
read = inputStream.read()
|
||||
}
|
||||
|
||||
with(decoder) {
|
||||
tryDecode(byteBuffer, charBuffer, true) // throws exception if undecoded bytes are left
|
||||
reset()
|
||||
}
|
||||
|
||||
with(charBuffer) {
|
||||
if (!contentEquals(lineSeparator)) {
|
||||
flip()
|
||||
while (hasRemaining()) stringBuilder.append(get())
|
||||
}
|
||||
}
|
||||
|
||||
return stringBuilder.toString()
|
||||
}
|
||||
|
||||
private fun CharsetDecoder.tryDecode(byteBuffer: ByteBuffer, charBuffer: CharBuffer, isEndOfStream: Boolean): Boolean {
|
||||
val positionBefore = charBuffer.position()
|
||||
byteBuffer.flip()
|
||||
with(decode(byteBuffer, charBuffer, isEndOfStream)) {
|
||||
if (isError) throwException()
|
||||
}
|
||||
return (charBuffer.position() > positionBefore).also { isDecoded ->
|
||||
byteBuffer.apply { if (isDecoded) clear() else flipBack() }
|
||||
}
|
||||
}
|
||||
|
||||
private fun CharBuffer.contentEquals(string: String): Boolean {
|
||||
flip()
|
||||
return string.contentEquals(this).also { flipBack() }
|
||||
}
|
||||
|
||||
private fun Buffer.flipBack(): Buffer = apply {
|
||||
position(limit())
|
||||
limit(capacity())
|
||||
}
|
||||
|
||||
private fun CharBuffer.dequeue(): Char {
|
||||
flip()
|
||||
return get().also { compact() }
|
||||
}
|
||||
Reference in New Issue
Block a user