-
- All Implemented Interfaces:
-
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public interface Sink implements Closeable, Flushable
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 OutputStreamThis interface is functionally equivalent to java.io.OutputStream.{@code OutputStream}requires multiple layers when emitted data is heterogeneous: a{@code DataOutputStream}for primitive values, a{@code * BufferedOutputStream}for buffering, and{@code OutputStreamWriter}for charset encoding. This class uses{@code BufferedSink}for all of the above.Sink is also easier to layer: there is no single-byte write method that is awkward to implement efficiently.
Interop with OutputStreamUse sink to adapt an{@code OutputStream}to a sink. Use to adapt a sink to an{@code OutputStream}.
-
-
Method Summary
Modifier and Type Method Description abstract voidwrite(Buffer source, long byteCount)Removes {@code byteCount}bytes from{@code source}and appends them to this.abstract voidflush()Pushes all buffered bytes to their final destination. abstract Timeouttimeout()Returns the timeout for this sink. abstract voidclose()Pushes all buffered bytes to their final destination and releases theresources held by this sink. -
-
Method Detail
-
write
abstract void write(Buffer source, long byteCount)
Removes
{@code byteCount}bytes from{@code source}and appends them to this.
-
flush
abstract void flush()
Pushes all buffered bytes to their final destination.
-
close
abstract void close()
Pushes all buffered bytes to their final destination and releases theresources held by this sink. It is an error to write a closed sink. It issafe to close a sink more than once.
-
-
-
-