public abstract class ByteSource extends Object
InputStream, a
ByteSource is not an open, stateful stream for input that can be read and closed. Instead, it is an immutable
supplier of
InputStream instances.
ByteSource provides two kinds of methods:
| Modifier | Constructor and Description |
|---|---|
protected |
ByteSource()
Constructor for use by subclasses.
|
| Modifier and Type | Method and Description |
|---|---|
CharSource |
asCharSource(Charset
Returns a
CharSource view of this byte source that decodes bytes read from this source as characters using the given
Charset.
|
static ByteSource |
concat(ByteSource
Concatenates multiple
ByteSource instances into a single source.
|
static ByteSource |
concat(Iterable
Concatenates multiple
ByteSource instances into a single source.
|
static ByteSource |
concat(Iterator
Concatenates multiple
ByteSource instances into a single source.
|
boolean |
contentEquals(ByteSource
Checks that the contents of this byte source are equal to the contents of the given byte source.
|
long |
copyTo(ByteSink
Copies the contents of this byte source to the given
ByteSink.
|
long |
copyTo(OutputStream
Copies the contents of this byte source to the given
OutputStream.
|
static ByteSource |
empty()
Returns an immutable
ByteSource that contains no bytes.
|
HashCode |
hash(HashFunction
Hashes the contents of this byte source using the given hash function.
|
boolean |
isEmpty()
Returns whether the source has zero bytes.
|
InputStream |
openBufferedStream()
Opens a new buffered
InputStream for reading from this source.
|
abstract InputStream |
openStream()
Opens a new
InputStream for reading from this source.
|
byte[] |
read()
Reads the full contents of this byte source as a byte array.
|
<T> T |
read(ByteProcessor
Reads the contents of this byte source using the given
processor to process bytes as they are read.
|
long |
size()
Returns the size of this source in bytes.
|
ByteSource |
slice(long offset, long length)
Returns a view of a slice of this byte source that is at most
length bytes long starting at the given
offset.
|
static ByteSource |
wrap(byte[] b)
Returns a view of the given byte array as a
ByteSource.
|
public CharSourceasCharSource(Charset charset)
CharSource view of this byte source that decodes bytes read from this source as characters using the given
Charset.
public abstract InputStreamopenStream() throws IOException
InputStream for reading from this source. This method should return a new, independent stream each time it is called.
The caller is responsible for ensuring that the returned stream is closed.
IOException - if an I/O error occurs in the process of opening the stream
public InputStreamopenBufferedStream() throws IOException
InputStream for reading from this source. The returned stream is not required to be a
BufferedInputStream in order to allow implementations to simply delegate to
openStream() when the stream returned by that method does not benefit from additional buffering (for example, a
ByteArrayInputStream). This method should return a new, independent stream each time it is called.
The caller is responsible for ensuring that the returned stream is closed.
IOException - if an I/O error occurs in the process of opening the stream
BufferedInputStream)
public ByteSourceslice(long offset, long length)
length bytes long starting at the given
offset.
IllegalArgumentException - if
offset or
length is negative
public boolean isEmpty()
throws IOException
IOException - if an I/O error occurs
public long size()
throws IOException
skip, if possible) to the end of the stream and return the total number of bytes that were read.
For some sources, such as a file, this method may use a more efficient implementation. Note that in such cases, it is possible that this method will return a different number of bytes than would be returned by reading all of the bytes (for example, some special files may return a size of 0 despite actually having content when read).
In either case, if this is a mutable source such as a file, the size it returns may not be the same number of bytes a subsequent read would return.
IOException - if an I/O error occurs in the process of reading the size of this source
public long copyTo(OutputStreamoutput) throws IOException
OutputStream. Does not close
output.
IOException - if an I/O error occurs in the process of reading from this source or writing to
output
public long copyTo(ByteSinksink) throws IOException
ByteSink.
IOException - if an I/O error occurs in the process of reading from this source or writing to
sink
public byte[] read()
throws IOException
IOException - if an I/O error occurs in the process of reading from this source
@Beta public <T> T read(ByteProcessor<T> processor) throws IOException
processor to process bytes as they are read. Stops when all bytes have been read or the consumer returns
false. Returns the result produced by the processor.
IOException - if an I/O error occurs in the process of reading from this source or if
processor throws an
IOException
public HashCodehash(HashFunction hashFunction) throws IOException
IOException - if an I/O error occurs in the process of reading from this source
public boolean contentEquals(ByteSourceother) throws IOException
IOException - if an I/O error occurs in the process of reading from this source or
other
public static ByteSourceconcat(Iterable <? extends ByteSource > sources)
ByteSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.
Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
sources - the sources to concatenate
ByteSource containing the concatenated data
public static ByteSourceconcat(Iterator <? extends ByteSource > sources)
ByteSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.
Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
Note: The input Iterator will be copied to an ImmutableList when this method is called. This will fail if the iterator is infinite and may cause problems if the iterator eagerly fetches data for each source when iterated (rather than producing sources that only load data through their streams). Prefer using the concat(Iterable) overload if possible.
sources - the sources to concatenate
ByteSource containing the concatenated data
NullPointerException - if any of
sources is
null
public static ByteSourceconcat(ByteSource ... sources)
ByteSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.
Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
sources - the sources to concatenate
ByteSource containing the concatenated data
NullPointerException - if any of
sources is
null
public static ByteSourcewrap(byte[] b)
ByteSource. To view only a specific range in the array, use
ByteSource.wrap(b).slice(offset, length).
ByteStreams.asByteSource(byte[])).
public static ByteSourceempty()
ByteSource that contains no bytes.