Sink

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.

interface Sink : Closeable, Flushable

Functions

close
Link copied to clipboard
common
abstract override fun close()
Pushes all buffered bytes to their final destination and releases the resources held by this sink.
abstract override fun close()
equals
Link copied to clipboard
open operator fun equals(other: Any?): Boolean
open operator fun equals(other: Any?): Boolean
flush
Link copied to clipboard
common
abstract fun flush()
Pushes all buffered bytes to their final destination.
abstract override fun flush()
hashCode
Link copied to clipboard
open fun hashCode(): Int
open fun hashCode(): Int
timeout
Link copied to clipboard
common
abstract fun timeout(): Timeout
Returns the timeout for this sink.
abstract fun timeout(): Timeout
toString
Link copied to clipboard
open fun toString(): String
open fun toString(): String
write
Link copied to clipboard
common
abstract fun write(source: Buffer, byteCount: Long)
Removes byteCount bytes from source and appends them to this.
abstract fun write(source: Buffer, byteCount: Long)

Inheritors

BufferedSink
Link copied to clipboard
HashingSink
Link copied to clipboard
CipherSink
Link copied to clipboard
DeflaterSink
Link copied to clipboard
ForwardingSink
Link copied to clipboard
GzipSink
Link copied to clipboard

Extensions

buffer
Link copied to clipboard
common
fun Sink.buffer(): BufferedSink
Returns a new sink that buffers writes to sink.
cipherSink
Link copied to clipboard
fun Sink.cipherSink(cipher: Cipher): CipherSink
Returns a sink that uses cipher to encrypt or decrypt this.
deflate
Link copied to clipboard
inline fun Sink.deflate(deflater: Deflater = Deflater()): DeflaterSink
Returns an DeflaterSink that DEFLATE-compresses data to this Sink while writing.
gzip
Link copied to clipboard
inline fun Sink.gzip(): GzipSink
Returns a GzipSink that gzip-compresses to this Sink while writing.
hashingSink
Link copied to clipboard
fun Sink.hashingSink(mac: Mac): HashingSink
Returns a sink that uses mac to hash this.
fun Sink.hashingSink(digest: MessageDigest): HashingSink
Returns a sink that uses digest to hash this.