Sink

expect interface Sink : Closeable

Receives a stream of bytes. Use this interface to write data wherever it's needed: to the network, storage, or a buffer in memory. Sinks may be layered to transform received data, such as to compress, encrypt, throttle, or add protocol framing.

Most application code shouldn't operate on a sink directly, but rather on a BufferedSink which is both more efficient and more convenient. Use buffer to wrap any sink with a buffer.

Sinks are easy to test: just use a Buffer in your tests, and read from it to confirm it received the data that was expected.

Comparison with OutputStream

This interface is functionally equivalent to java.io.OutputStream.

OutputStream requires multiple layers when emitted data is heterogeneous: a DataOutputStream for primitive values, a BufferedOutputStream for buffering, and OutputStreamWriter for charset encoding. This library uses BufferedSink for all of the above.

Sink is also easier to layer: there is no java.io.OutputStream.write method that is awkward to implement efficiently.

Interop with OutputStream

Use sink to adapt an OutputStream to a sink. Use BufferedSink.outputStream to adapt a sink to an OutputStream.

Inheritors

actual interface Sink : Closeable

Inheritors

Functions

Link copied to clipboard

Returns a new sink that buffers writes to sink. The returned sink will batch writes to sink. Use this wherever you write to a sink to get an ergonomic and efficient access to data.

Link copied to clipboard

Returns a sink that uses cipher to encrypt or decrypt this.

Link copied to clipboard
expect abstract override fun close()

Pushes all buffered bytes to their final destination and releases the resources held by this sink. It is an error to write a closed sink. It is safe to close a sink more than once.

actual abstract override fun close()
actual abstract override fun close()
Link copied to clipboard
inline fun Sink.deflate(deflater: Deflater = Deflater()): DeflaterSink

Returns an DeflaterSink that DEFLATE-compresses data to this Sink while writing.

Link copied to clipboard
expect abstract fun flush()

Pushes all buffered bytes to their final destination.

actual abstract override fun flush()
actual abstract fun flush()
Link copied to clipboard
inline fun Sink.gzip(): GzipSink

Returns a GzipSink that gzip-compresses to this Sink while writing.

Link copied to clipboard

Returns a sink that uses digest to hash this.

Returns a sink that uses mac to hash this.

Link copied to clipboard
expect abstract fun timeout(): Timeout

Returns the timeout for this sink.

actual abstract fun timeout(): Timeout
actual abstract fun timeout(): Timeout
Link copied to clipboard
expect abstract fun write(source: Buffer, byteCount: Long)

Removes byteCount bytes from source and appends them to this.

actual abstract fun write(source: Buffer, byteCount: Long)
actual abstract fun write(source: Buffer, byteCount: Long)