readByteArray

Removes all bytes from this source and returns them as a byte array.

Throws

when the source is closed.

Samples

import kotlinx.io.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val buffer = Buffer()
buffer.write(byteArrayOf(1, 2, 3, 4, 5, 6, 7))

val out = ByteArray(10)
buffer.readTo(out, startIndex = 1, endIndex = 4)
assertContentEquals(byteArrayOf(0, 1, 2, 3, 0, 0, 0, 0, 0, 0), out)
assertContentEquals(byteArrayOf(4, 5, 6, 7), buffer.readByteArray()) 
   //sampleEnd
}

Removes byteCount bytes from this source and returns them as a byte array.

Parameters

byteCount

the number of bytes that should be read from the source.

Throws

when the underlying source is exhausted before byteCount bytes of data could be read.

when the source is closed.

Samples

import kotlinx.io.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val buffer = Buffer()
buffer.write(byteArrayOf(1, 2, 3, 4, 5, 6, 7))

val out = ByteArray(10)
buffer.readTo(out, startIndex = 1, endIndex = 4)
assertContentEquals(byteArrayOf(0, 1, 2, 3, 0, 0, 0, 0, 0, 0), out)
assertContentEquals(byteArrayOf(4, 5, 6, 7), buffer.readByteArray()) 
   //sampleEnd
}