emit

abstract fun emit()(source)

Writes all buffered data to the underlying sink if one exists. The underlying sink will not be explicitly flushed.

This method behaves like flush, but has weaker guarantees. Call this method before a buffered sink goes out of scope so that its data can reach its destination.

Throws

when the sink is closed.

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.emit()
// The sink was filled with buffered data, but it was not flushed
assertFalse(rawSink.flushed)
assertContentEquals(byteArrayOf(0x10, 0), rawSink.buffer.readByteArray()) 
   //sampleEnd
}