readAtMostTo
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
the array to which data will be written from this source.
the startIndex (inclusive) of the sink subrange to read data into, 0 by default.
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.
when some I/O error occurs.
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
}
Removes at least 1, and up to byteCount bytes from this source and appends them to sink. Returns the number of bytes read, or -1 if this source is exhausted.
Parameters
the destination to write the data from this source.
the number of bytes to read.
Throws
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 = Buffer()
val bytesRead = source.readAtMostTo(sink, 10) // read at most 10 bytes
assertEquals(6, bytesRead)
assertContentEquals(byteArrayOf(1, 2, 3, 4, 5, 6), sink.readByteArray())
//sampleEnd
}