• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package okio;
17 
18 import java.io.Closeable;
19 import java.io.Flushable;
20 import java.io.IOException;
21 
22 /**
23  * Receives a stream of bytes. Use this interface to write data wherever it's
24  * needed: to the network, storage, or a buffer in memory. Sinks may be layered
25  * to transform received data, such as to compress, encrypt, throttle, or add
26  * protocol framing.
27  *
28  * <p>Most application code shouldn't operate on a sink directly, but rather
29  * {@link BufferedSink} which is both more efficient and more convenient. Use
30  * {@link Okio#buffer(Sink)} to wrap any sink with a buffer.
31  *
32  * <p>Sinks are easy to test: just use an {@link Buffer} in your tests, and
33  * read from it to confirm it received the data that was expected.
34  *
35  * <h3>Comparison with OutputStream</h3>
36  * This interface is functionally equivalent to {@link java.io.OutputStream}.
37  *
38  * <p>{@code OutputStream} requires multiple layers when emitted data is
39  * heterogeneous: a {@code DataOutputStream} for primitive values, a {@code
40  * BufferedOutputStream} for buffering, and {@code OutputStreamWriter} for
41  * charset encoding. This class uses {@code BufferedSink} for all of the above.
42  *
43  * <p>Sink is also easier to layer: there is no {@linkplain
44  * java.io.OutputStream#write(int) single-byte write} method that is awkward to
45  * implement efficiently.
46  *
47  * <h3>Interop with OutputStream</h3>
48  * Use {@link Okio#sink} to adapt an {@code OutputStream} to a sink. Use {@link
49  * BufferedSink#outputStream} to adapt a sink to an {@code OutputStream}.
50  */
51 public interface Sink extends Closeable, Flushable {
52   /** Removes {@code byteCount} bytes from {@code source} and appends them to this. */
write(Buffer source, long byteCount)53   void write(Buffer source, long byteCount) throws IOException;
54 
55   /** Pushes all buffered bytes to their final destination. */
flush()56   @Override void flush() throws IOException;
57 
58   /** Returns the timeout for this sink. */
timeout()59   Timeout timeout();
60 
61   /**
62    * Pushes all buffered bytes to their final destination and releases the
63    * resources held by this sink. It is an error to write a closed sink. It is
64    * safe to close a sink more than once.
65    */
close()66   @Override void close() throws IOException;
67 }
68