write

open override fun write(source: ByteArray, startIndex: Int, endIndex: Int)(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.

when some I/O error occurs.

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
}

open override 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.

when some I/O error occurs.

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
}

open override fun write(source: Buffer, byteCount: Long)(source)

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

Parameters

source

the source to read data from.

byteCount

the number of bytes to write.

Throws

when the source's size is below byteCount or byteCount is negative.

when the sink is closed.

when some I/O error occurs.