flush

abstract override fun flush()(source)

Writes all buffered data to the underlying sink, if one exists. Then the underlying sink is explicitly flushed.

Throws

when the sink is closed.

when some I/O error occurs.

Samples

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

fun main() { 
   //sampleStart 
   val rawSink = object : RawSink {
    val buffer = Buffer()
    var flushed = false
    override fun write(source: Buffer, byteCount: Long) {
        source.readTo(buffer, byteCount)
    }

    override fun flush() {
        flushed = true
    }

    override fun close() = Unit
}

val buffered = rawSink.buffered()

buffered.writeShort(0x1000)
// Not data were sent to the underlying sink and it was not flushed yet
assertFalse(rawSink.flushed)
assertEquals(0, rawSink.buffer.size)

buffered.flush()
// The sink was filled with buffered data and then flushed
assertTrue(rawSink.flushed)
assertContentEquals(byteArrayOf(0x10, 0), rawSink.buffer.readByteArray()) 
   //sampleEnd
}