Package okio
Interface Sink
-
- All Superinterfaces:
java.lang.AutoCloseable
,java.io.Closeable
,java.io.Flushable
- All Known Subinterfaces:
BufferedSink
- All Known Implementing Classes:
Buffer
,DeflaterSink
,ForwardingSink
,GzipSink
,HashingSink
public interface Sink extends java.io.Closeable, java.io.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. UseOkio.buffer(Sink)
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 toOutputStream
.OutputStream
requires multiple layers when emitted data is heterogeneous: aDataOutputStream
for primitive values, aBufferedOutputStream
for buffering, andOutputStreamWriter
for charset encoding. This class usesBufferedSink
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 OutputStream
UseOkio.sink(java.io.OutputStream)
to adapt anOutputStream
to a sink. UseBufferedSink.outputStream()
to adapt a sink to anOutputStream
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
close()
Pushes all buffered bytes to their final destination and releases the resources held by this sink.void
flush()
Pushes all buffered bytes to their final destination.Timeout
timeout()
Returns the timeout for this sink.void
write(Buffer source, long byteCount)
RemovesbyteCount
bytes fromsource
and appends them to this.
-
-
-
Method Detail
-
write
void write(Buffer source, long byteCount) throws java.io.IOException
RemovesbyteCount
bytes fromsource
and appends them to this.- Throws:
java.io.IOException
-
flush
void flush() throws java.io.IOException
Pushes all buffered bytes to their final destination.- Specified by:
flush
in interfacejava.io.Flushable
- Throws:
java.io.IOException
-
timeout
Timeout timeout()
Returns the timeout for this sink.
-
close
void close() throws java.io.IOException
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.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
-