okio / okio / Sink

Sink

(js, jvm, native) interface Sink(js)

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 write() method that is awkward to implement efficiently.

Interop with OutputStream

Use sink to adapt an OutputStream to a sink. Use outputStream() to adapt a sink to an OutputStream.

(jvm)

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 write() method that is awkward to implement efficiently.

Interop with OutputStream

Use sink to adapt an OutputStream to a sink. Use outputStream() to adapt a sink to an OutputStream.

Functions

Name Summary
(js, jvm, native) 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.abstract fun close():Unit
(js, jvm, native) flush Pushes all buffered bytes to their final destination.abstract fun flush():Unit
(js, jvm, native) timeout Returns the timeout for this sink.abstract fun timeout():Timeout
(js, jvm, native) write Removes byteCount bytes from source and appends them to this.abstract fun write(source:Buffer, byteCount:Long):Unit

Extension Functions

Name Summary
(jvm) deflate Returns an DeflaterSink that DEFLATE-compresses data to this Sink while writing.funSink.deflate(deflater:Deflater= Deflater()):DeflaterSink
(jvm) gzip Returns a GzipSink that gzip-compresses to this Sink while writing.funSink.gzip():GzipSink

Inheritors

Name Summary
(js, jvm, native) BufferedSink A sink that keeps a buffer internally so that callers can do small writes without a performance penalty.interface BufferedSink :Sink
(jvm) DeflaterSink A sink that uses DEFLATE to compress data written to another source.class DeflaterSink :Sink
(jvm) ForwardingSink A Sink which forwards calls to another. Useful for subclassing.abstract class ForwardingSink :Sink
(jvm) GzipSink A sink that uses GZIP to compress written data to another sink.class GzipSink :Sink