13c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/* 23c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Copyright (C) 2014 Square, Inc. 33c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 43c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Licensed under the Apache License, Version 2.0 (the "License"); 53c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * you may not use this file except in compliance with the License. 63c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * You may obtain a copy of the License at 73c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 83c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * http://www.apache.org/licenses/LICENSE-2.0 93c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Unless required by applicable law or agreed to in writing, software 113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * distributed under the License is distributed on an "AS IS" BASIS, 123c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * See the License for the specific language governing permissions and 143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * limitations under the License. 153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpackage okio; 173c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.Closeable; 193be78b8b0ca13d9e05e2327acb8d8654f719a3f6Neil Fullerimport java.io.Flushable; 203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.IOException; 213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/** 233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Receives a stream of bytes. Use this interface to write data wherever it's 243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * needed: to the network, storage, or a buffer in memory. Sinks may be layered 253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * to transform received data, such as to compress, encrypt, throttle, or add 263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * protocol framing. 273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 283c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * <p>Most application code shouldn't operate on a sink directly, but rather 293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * {@link BufferedSink} which is both more efficient and more convenient. Use 303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * {@link Okio#buffer(Sink)} to wrap any sink with a buffer. 313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 32e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * <p>Sinks are easy to test: just use an {@link Buffer} in your tests, and 333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * read from it to confirm it received the data that was expected. 343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * <h3>Comparison with OutputStream</h3> 363c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * This interface is functionally equivalent to {@link java.io.OutputStream}. 373c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * <p>{@code OutputStream} requires multiple layers when emitted data is 393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * heterogeneous: a {@code DataOutputStream} for primitive values, a {@code 403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * BufferedOutputStream} for buffering, and {@code OutputStreamWriter} for 413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * charset encoding. This class uses {@code BufferedSink} for all of the above. 423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 43e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller * <p>Sink is also easier to layer: there is no {@linkplain 443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * java.io.OutputStream#write(int) single-byte write} method that is awkward to 453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * implement efficiently. 463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * <h3>Interop with OutputStream</h3> 483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Use {@link Okio#sink} to adapt an {@code OutputStream} to a sink. Use {@link 493c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * BufferedSink#outputStream} to adapt a sink to an {@code OutputStream}. 503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 513be78b8b0ca13d9e05e2327acb8d8654f719a3f6Neil Fullerpublic interface Sink extends Closeable, Flushable { 523c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller /** Removes {@code byteCount} bytes from {@code source} and appends them to this. */ 53e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller void write(Buffer source, long byteCount) throws IOException; 543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller /** Pushes all buffered bytes to their final destination. */ 563be78b8b0ca13d9e05e2327acb8d8654f719a3f6Neil Fuller @Override void flush() throws IOException; 573c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 58e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller /** Returns the timeout for this sink. */ 59e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller Timeout timeout(); 603c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 613c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller /** 623c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Pushes all buffered bytes to their final destination and releases the 633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * resources held by this sink. It is an error to write a closed sink. It is 643c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * safe to close a sink more than once. 653c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 663c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override void close() throws IOException; 673c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller} 68