Package okio
Interface Sink
-
- All Superinterfaces:
AutoCloseable,Closeable,Flushable
- All Known Subinterfaces:
BufferedSink
- All Known Implementing Classes:
Buffer,DeflaterSink,ForwardingSink,GzipSink,RetryableSink
public interface Sink extends 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
BufferedSinkwhich is both more efficient and more convenient. UseOkio.buffer(Sink)to wrap any sink with a buffer.Sinks are easy to test: just use an
Bufferin your tests, and read from it to confirm it received the data that was expected.Comparison with OutputStream
This interface is functionally equivalent toOutputStream.OutputStreamrequires multiple layers when emitted data is heterogeneous: aDataOutputStreamfor primitive values, aBufferedOutputStreamfor buffering, andOutputStreamWriterfor charset encoding. This class usesBufferedSinkfor 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 OutputStream
UseOkio.sink(java.io.OutputStream)to adapt anOutputStreamto a sink. UseBufferedSink.outputStream()to adapt a sink to anOutputStream.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidclose()Pushes all buffered bytes to their final destination and releases the resources held by this sink.voidflush()Pushes all buffered bytes to their final destination.Timeouttimeout()Returns the timeout for this sink.voidwrite(Buffer source, long byteCount)RemovesbyteCountbytes fromsourceand appends them to this.
-
-
-
Method Detail
-
write
void write(Buffer source, long byteCount) throws IOException
RemovesbyteCountbytes fromsourceand appends them to this.- Throws:
IOException
-
flush
void flush() throws IOExceptionPushes all buffered bytes to their final destination.- Specified by:
flushin interfaceFlushable- Throws:
IOException- if there are any issues writing the data.
-
timeout
Timeout timeout()
Returns the timeout for this sink.
-
close
void close() throws IOExceptionPushes 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.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
-