readAtMostTo

open override fun readAtMostTo(sink: ByteArray, startIndex: Int, endIndex: Int): 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
}

open override fun readAtMostTo(sink: Buffer, byteCount: Long): Long(source)

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

sink

the destination to write the data from this source.

byteCount

the number of bytes to read.

Throws

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 = 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
}