接口 Sink

所有超级接口:
AutoCloseable, Closeable, Flushable
所有已知子接口:
BufferedSink
所有已知实现类:
Buffer, DeflaterSink, ForwardingSink, GzipSink, HashingSink

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 on a BufferedSink which is both more efficient and more convenient. Use Okio.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 to 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 class uses 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 OutputStream

Use Okio.sink(java.io.OutputStream) to adapt an OutputStream to a sink. Use BufferedSink.outputStream() to adapt a sink to an OutputStream.
  • 方法概要

    修饰符和类型
    方法
    说明
    void
    Pushes all buffered bytes to their final destination and releases the resources held by this sink.
    void
    Pushes all buffered bytes to their final destination.
    Returns the timeout for this sink.
    void
    write(Buffer source, long byteCount)
    Removes byteCount bytes from source and appends them to this.
  • 方法详细资料

    • write

      void write(Buffer source, long byteCount) throws IOException
      Removes byteCount bytes from source and appends them to this.
      抛出:
      IOException
    • flush

      void flush() throws IOException
      Pushes all buffered bytes to their final destination.
      指定者:
      flush 在接口中 Flushable
      抛出:
      IOException
    • timeout

      Timeout timeout()
      Returns the timeout for this sink.
    • close

      void close() throws 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.
      指定者:
      close 在接口中 AutoCloseable
      指定者:
      close 在接口中 Closeable
      抛出:
      IOException