readAtMostTo
abstract fun readAtMostTo(sink: ByteArray, startIndex: Int = 0, endIndex: Int = sink.size): Int(source)
Removes up to endIndex - startIndex
bytes from this source, copies them into sink subrange starting at startIndex and ending at endIndex, and returns the number of bytes read, or -1 if this source is exhausted.
Parameters
sink
the array to which data will be written from this source.
startIndex
the startIndex (inclusive) of the sink subrange to read data into, 0 by default.
endIndex
the endIndex (exclusive) of the sink subrange to read data into, sink.size
by default.
Throws
when startIndex or endIndex is out of range of sink array indices.
when startIndex > endIndex
.
when the source is closed.
Samples
import kotlinx.io.*
import kotlin.test.*
fun main() {
//sampleStart
val source = Buffer().also { it.write(byteArrayOf(1, 2, 3, 4, 5, 6)) }
val sink = ByteArray(10)
val bytesRead = source.readAtMostTo(sink) // read at most 10 bytes
assertEquals(6, bytesRead)
assertContentEquals(byteArrayOf(1, 2, 3, 4, 5, 6, 0, 0, 0, 0), sink)
//sampleEnd
}