request
Attempts to fill the buffer with at least byteCount bytes of data from the underlying source and returns a value indicating if the requirement was successfully fulfilled.
false
value returned by this method indicates that the underlying source was exhausted before filling the buffer with byteCount bytes of data.
Parameters
byteCount
the number of bytes that the buffer should contain.
Throws
when byteCount is negative.
when the source is closed.
Samples
import kotlinx.io.*
import kotlin.test.*
fun main() {
//sampleStart
val singleByteSource = object : RawSource {
private var exhausted = false
override fun readAtMostTo(sink: Buffer, byteCount: Long): Long = when {
byteCount == 0L -> 0L
exhausted -> -1L
else -> {
exhausted = true
sink.writeByte(42)
1L
}
}
override fun close() = Unit
}
val source = singleByteSource.buffered()
assertTrue(source.request(1))
// The request call already soaked all the data from the source
assertEquals(-1, singleByteSource.readAtMostTo(Buffer(), 1))
// There is only one byte, so we can't request more
assertFalse(source.request(2))
// But we didn't consume single byte yet, so request(1) will succeed
assertTrue(source.request(1))
assertEquals(42, source.readByte())
assertFalse(source.request(1))
//sampleEnd
}