Class ByteSource
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:
 
- Methods that return a stream: These methods should return a new, independent instance each time they are called. The caller is responsible for ensuring that the returned stream is closed.
- Convenience methods: These are implementations of common operations that are typically implemented by opening a stream using one of the methods in the first category, doing something and finally closing the stream that was opened.
Note: In general, ByteSource is intended to be used for "file-like" sources
 that provide streams that are:
 
- Finite: Many operations, such as size()andread(), will either block indefinitely or fail if the source creates an infinite stream.
- Non-destructive: A destructive stream will consume or otherwise alter the
       bytes of the source as they are read from it. A source that provides such streams will not
       be reusable, and operations that read from the stream (including size(), in some implementations) will prevent further operations from completing as expected.
- Since:
- 14.0
- Author:
- Colin Decker
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionasCharSource(Charset charset) Returns aCharSourceview of this byte source that decodes bytes read from this source as characters using the givenCharset.static ByteSourceconcat(ByteSource... sources) Concatenates multipleByteSourceinstances into a single source.static ByteSourceconcat(Iterable<? extends ByteSource> sources) Concatenates multipleByteSourceinstances into a single source.static ByteSourceconcat(Iterator<? extends ByteSource> sources) Concatenates multipleByteSourceinstances into a single source.booleancontentEquals(ByteSource other) Checks that the contents of this byte source are equal to the contents of the given byte source.longCopies the contents of this byte source to the givenByteSink.longcopyTo(OutputStream output) Copies the contents of this byte source to the givenOutputStream.static ByteSourceempty()Returns an immutableByteSourcethat contains no bytes.hash(HashFunction hashFunction) Hashes the contents of this byte source using the given hash function.booleanisEmpty()Returns whether the source has zero bytes.Opens a new bufferedInputStreamfor reading from this source.abstract InputStreamOpens a newInputStreamfor reading from this source.byte[]read()Reads the full contents of this byte source as a byte array.read(ByteProcessor<T> processor) Reads the contents of this byte source using the givenprocessorto process bytes as they are read.longsize()Returns the size of this source in bytes, even if doing so requires opening and traversing an entire stream.Returns the size of this source in bytes, if the size can be easily determined without actually opening the data stream.slice(long offset, long length) Returns a view of a slice of this byte source that is at mostlengthbytes long starting at the givenoffset.static ByteSourcewrap(byte[] b) Returns a view of the given byte array as aByteSource.
- 
Constructor Details- 
ByteSourceprotected ByteSource()Constructor for use by subclasses.
 
- 
- 
Method Details- 
asCharSourceReturns aCharSourceview of this byte source that decodes bytes read from this source as characters using the givenCharset.If CharSource.asByteSource(java.nio.charset.Charset)is called on the returned source with the same charset, the default implementation of this method will ensure that the originalByteSourceis returned, rather than round-trip encoding. Subclasses that override this method should behave the same way.
- 
openStreamOpens a newInputStreamfor reading from this source. This method returns a new, independent stream each time it is called.The caller is responsible for ensuring that the returned stream is closed. - Throws:
- IOException- if an I/O error occurs while opening the stream
 
- 
openBufferedStreamOpens a new bufferedInputStreamfor reading from this source. The returned stream is not required to be aBufferedInputStreamin order to allow implementations to simply delegate toopenStream()when the stream returned by that method does not benefit from additional buffering (for example, aByteArrayInputStream). This method returns a new, independent stream each time it is called.The caller is responsible for ensuring that the returned stream is closed. - Throws:
- IOException- if an I/O error occurs while opening the stream
- Since:
- 15.0 (in 14.0 with return type BufferedInputStream)
 
- 
sliceReturns a view of a slice of this byte source that is at mostlengthbytes long starting at the givenoffset. Ifoffsetis greater than the size of this source, the returned source will be empty. Ifoffset + lengthis greater than the size of this source, the returned source will contain the slice starting atoffsetand ending at the end of this source.- Throws:
- IllegalArgumentException- if- offsetor- lengthis negative
 
- 
isEmptyReturns whether the source has zero bytes. The default implementation first checkssizeIfKnown(), returning true if it's known to be zero and false if it's known to be non-zero. If the size is not known, it falls back to opening a stream and checking for EOF.Note that, in cases where sizeIfKnownreturns zero, it is possible that bytes are actually available for reading. (For example, some special files may return a size of 0 despite actually having content when read.) This means that a source may returntruefromisEmpty()despite having readable content.- Throws:
- IOException- if an I/O error occurs
- Since:
- 15.0
 
- 
sizeIfKnownReturns the size of this source in bytes, if the size can be easily determined without actually opening the data stream.The default implementation returns Optional.absent(). Some sources, such as a file, may return a non-absent value. 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).Additionally, for mutable sources such as files, a subsequent read may return a different number of bytes if the contents are changed. - Since:
- 19.0
 
- 
sizeReturns the size of this source in bytes, even if doing so requires opening and traversing an entire stream. To avoid a potentially expensive operation, seesizeIfKnown().The default implementation calls sizeIfKnown()and returns the value if present. If absent, it will fall back to a heavyweight operation that will open a stream, read (orskip, if possible) to the end of the stream and return the total number of bytes that were read.Note that for some sources that implement sizeIfKnown()to provide a more efficient implementation, 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, for mutable sources such as files, a subsequent read may return a different number of bytes if the contents are changed. - Throws:
- IOException- if an I/O error occurs while reading the size of this source
 
- 
copyToCopies the contents of this byte source to the givenOutputStream. Does not closeoutput.- Returns:
- the number of bytes copied
- Throws:
- IOException- if an I/O error occurs while reading from this source or writing to- output
 
- 
copyToCopies the contents of this byte source to the givenByteSink.- Returns:
- the number of bytes copied
- Throws:
- IOException- if an I/O error occurs while reading from this source or writing to- sink
 
- 
readReads the full contents of this byte source as a byte array.- Throws:
- IOException- if an I/O error occurs while reading from this source
 
- 
read@CanIgnoreReturnValue public <T extends @Nullable Object> T read(ByteProcessor<T> processor) throws IOException Reads the contents of this byte source using the givenprocessorto process bytes as they are read. Stops when all bytes have been read or the consumer returnsfalse. Returns the result produced by the processor.- Throws:
- IOException- if an I/O error occurs while reading from this source or if- processorthrows an- IOException
- Since:
- 16.0
 
- 
hashHashes the contents of this byte source using the given hash function.- Throws:
- IOException- if an I/O error occurs while reading from this source
 
- 
contentEqualsChecks that the contents of this byte source are equal to the contents of the given byte source.- Throws:
- IOException- if an I/O error occurs while reading from this source or- other
 
- 
concatConcatenates multipleByteSourceinstances 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. - Parameters:
- sources- the sources to concatenate
- Returns:
- a ByteSourcecontaining the concatenated data
- Since:
- 15.0
 
- 
concatConcatenates multipleByteSourceinstances 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 Iteratorwill be copied to anImmutableListwhen 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 theconcat(Iterable)overload if possible.- Parameters:
- sources- the sources to concatenate
- Returns:
- a ByteSourcecontaining the concatenated data
- Throws:
- NullPointerException- if any of- sourcesis- null
- Since:
- 15.0
 
- 
concatConcatenates multipleByteSourceinstances 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. - Parameters:
- sources- the sources to concatenate
- Returns:
- a ByteSourcecontaining the concatenated data
- Throws:
- NullPointerException- if any of- sourcesis- null
- Since:
- 15.0
 
- 
wrapReturns a view of the given byte array as aByteSource. To view only a specific range in the array, useByteSource.wrap(b).slice(offset, length).Note that the given byte array may be passed directly to methods on, for example, OutputStream(whencopyTo(OutputStream)is called on the resultingByteSource). This could allow a maliciousOutputStreamimplementation to modify the contents of the array, but provides better performance in the normal case.- Since:
- 15.0 (since 14.0 as ByteStreams.asByteSource(byte[])).
 
- 
emptyReturns an immutableByteSourcethat contains no bytes.- Since:
- 15.0
 
 
-