readByteString

Consumes all bytes from this source and wraps it into a byte string.

Throws

if the source is closed.

when some I/O error occurs.

Samples

import kotlinx.io.*
import kotlinx.io.bytestring.ByteString
import kotlinx.io.bytestring.encodeToByteString
import kotlin.test.*

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

assertEquals(ByteString(1, 2), buffer.readByteString(2)) // reads only two bytes
assertEquals(ByteString(3, 4, 5), buffer.readByteString()) // reads until exhaustion
assertTrue(buffer.exhausted()) 
   //sampleEnd
}

Consumes exactly byteCount bytes from this source and wraps it into a byte string.

Parameters

byteCount

the number of bytes to read from the source.

Throws

when the source is exhausted before reading byteCount bytes from it.

if the source is closed.

when some I/O error occurs.

Samples

import kotlinx.io.*
import kotlinx.io.bytestring.ByteString
import kotlinx.io.bytestring.encodeToByteString
import kotlin.test.*

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

assertEquals(ByteString(1, 2), buffer.readByteString(2)) // reads only two bytes
assertEquals(ByteString(3, 4, 5), buffer.readByteString()) // reads until exhaustion
assertTrue(buffer.exhausted()) 
   //sampleEnd
}