接口 Source
- 所有超级接口:
AutoCloseable,Closeable
- 所有已知子接口:
BufferedSource
- 所有已知实现类:
Buffer,ForwardingSource,GzipSource,HashingSource,InflaterSource
Most applications shouldn't operate on a source directly, but rather on a
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 a Buffer in your tests, and
fill it with the data your application is to read.
Comparison with InputStream
This interface is functionally equivalent toInputStream.
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; instead, callers just buffer what
they need.
When implementing a source, you don't need to worry about the single-byte read method that is awkward to implement efficiently and returns one of 257 possible values.
And source has a stronger skip method: BufferedSource.skip(long)
won't return prematurely.
Interop with InputStream
UseOkio.source(java.io.InputStream) to adapt an InputStream to a source. Use BufferedSource.inputStream() to adapt a source to an InputStream.-
方法概要
-
方法详细资料
-
read
Removes at least 1, and up tobyteCountbytes from this and appends them tosink. Returns the number of bytes read, or -1 if this source is exhausted.- 抛出:
IOException
-
timeout
Timeout timeout()Returns the timeout for this source. -
close
Closes this source and releases the resources held by this source. It is an error to read a closed source. It is safe to close a source more than once.- 指定者:
close在接口中AutoCloseable- 指定者:
close在接口中Closeable- 抛出:
IOException
-