write

abstract fun write(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size)(source)

Writes bytes from source array or its subrange to this sink.

Parameters

source

the array from which bytes will be written into this sink.

startIndex

the start index (inclusive) of the source subrange to be written, 0 by default.

endIndex

the endIndex (exclusive) of the source subrange to be written, size of the source by default.

Throws

when startIndex or endIndex is out of range of source array indices.

when startIndex > endIndex.

when the sink is closed.

Samples

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

fun main() { 
   //sampleStart 
   val sink = Buffer()

sink.write(byteArrayOf(1, 2, 3, 4))
assertContentEquals(byteArrayOf(1, 2, 3, 4), sink.readByteArray())

sink.write(byteArrayOf(1, 2, 3, 4), startIndex = 1, endIndex = 3)
assertContentEquals(byteArrayOf(2, 3), sink.readByteArray()) 
   //sampleEnd
}

abstract fun write(source: RawSource, byteCount: Long)(source)

Removes byteCount bytes from source and write them to this sink.

If source will be exhausted before reading byteCount from it then an exception throws on an attempt to read remaining bytes will be propagated to a caller of this method.

Parameters

source

the source to consume data from.

byteCount

the number of bytes to read from source and to write into this sink.

Throws

when the sink or source is closed.

Samples

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

fun main() { 
   //sampleStart 
   val sink = Buffer()
val source = Buffer().also { it.writeInt(0x01020304) }

sink.write(source, 3)
assertContentEquals(byteArrayOf(1, 2, 3), sink.readByteArray())
assertContentEquals(byteArrayOf(4), source.readByteArray()) 
   //sampleEnd
}