add method ByteBuffer.read(InputStream)

This commit is contained in:
tursom 2020-01-21 12:42:34 +08:00
parent 3a11dac3b4
commit bfebd1e5c6

View File

@ -2,6 +2,7 @@ package cn.tursom.core.buffer
import cn.tursom.core.forEachIndex
import java.io.Closeable
import java.io.InputStream
import java.io.OutputStream
import kotlin.math.min
@ -198,6 +199,17 @@ interface ByteBuffer : Closeable {
array.forEachIndex(index, index + size - 1, this::put)
}
fun put(inputStream: InputStream) {
if (hasArray) {
val read = inputStream.read(array, writeOffset, writeable)
writePosition += read
} else {
val buffer = ByteArray(10 * 1024)
val read = inputStream.read(buffer)
put(buffer, 0, read)
}
}
fun putByte(byte: Byte): Unit = put(byte)
fun putChar(char: Char): Unit = put(char)
fun putShort(short: Short): Unit = put(short)