public interface Source
extends java.io.Closeable
Most applications shouldn't operate on a source directly, but rather
BufferedSource which is both more efficient and more convenient. Use
Okio.buffer(Source) to wrap any source with a buffer.
Sources are easy to test: just use an Buffer in your tests, and
fill it with the data your application is to read.
InputStream.
InputStream requires multiple layers when consumed data is
heterogeneous: a DataInputStream for primitive values, a BufferedInputStream for buffering, and InputStreamReader for
strings. This class uses BufferedSource for all of the above.
Source avoids the impossible-to-implement available() method. Instead callers specify
how many bytes they require.
Source omits the unsafe-to-compose mark and reset state that's tracked by InputStream; callers instead
just buffer what they need.
When implementing a source, you need not worry about the single-byte read method that is awkward to implement efficiently and that returns one of 257 possible values.
And source has a stronger skip method: BufferedSource.skip(long)
won't return prematurely.
Okio.source(java.io.InputStream) to adapt an InputStream to a source. Use
BufferedSource.inputStream() to adapt a source to an InputStream.long read(Buffer sink, long byteCount) throws java.io.IOException
byteCount bytes from this and appends
them to sink. Returns the number of bytes read, or -1 if this
source is exhausted.java.io.IOExceptionTimeout timeout()
void close()
throws java.io.IOException
close 在接口中 java.lang.AutoCloseableclose 在接口中 java.io.Closeablejava.io.IOException