Class ByteSource.ByteArrayByteSource
- Direct Known Subclasses:
ByteSource.EmptyByteSource
- Enclosing class:
ByteSource
-
Nested Class Summary
Nested classes/interfaces inherited from class com.google.common.io.ByteSource
ByteSource.AsCharSource
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionByteArrayByteSource
(byte[] bytes) ByteArrayByteSource
(byte[] bytes, int offset, int length) -
Method Summary
Modifier and TypeMethodDescriptionlong
copyTo
(OutputStream output) Copies the contents of this byte source to the givenOutputStream
.hash
(HashFunction hashFunction) Hashes the contents of this byte source using the given hash function.boolean
isEmpty()
Returns whether the source has zero bytes.Opens a new bufferedInputStream
for reading from this source.Opens a newInputStream
for reading from this source.byte[]
read()
Reads the full contents of this byte source as a byte array.<T> T
read
(ByteProcessor<T> processor) Reads the contents of this byte source using the givenprocessor
to process bytes as they are read.long
size()
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 mostlength
bytes long starting at the givenoffset
.toString()
Methods inherited from class com.google.common.io.ByteSource
asCharSource, concat, concat, concat, contentEquals, copyTo, empty, wrap
-
Field Details
-
bytes
final byte[] bytes -
offset
final int offset -
length
final int length
-
-
Constructor Details
-
ByteArrayByteSource
ByteArrayByteSource(byte[] bytes) -
ByteArrayByteSource
ByteArrayByteSource(byte[] bytes, int offset, int length)
-
-
Method Details
-
openStream
Description copied from class:ByteSource
Opens a newInputStream
for 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.
- Specified by:
openStream
in classByteSource
-
openBufferedStream
Description copied from class:ByteSource
Opens a new bufferedInputStream
for reading from this source. The returned stream is not required to be aBufferedInputStream
in order to allow implementations to simply delegate toByteSource.openStream()
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.
- Overrides:
openBufferedStream
in classByteSource
-
isEmpty
public boolean isEmpty()Description copied from class:ByteSource
Returns whether the source has zero bytes. The default implementation first checksByteSource.sizeIfKnown()
, 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
sizeIfKnown
returns 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 returntrue
fromisEmpty()
despite having readable content.- Overrides:
isEmpty
in classByteSource
-
size
public long size()Description copied from class:ByteSource
Returns the size of this source in bytes, even if doing so requires opening and traversing an entire stream. To avoid a potentially expensive operation, seeByteSource.sizeIfKnown()
.The default implementation calls
ByteSource.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
ByteSource.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.
- Overrides:
size
in classByteSource
-
sizeIfKnown
Description copied from class:ByteSource
Returns 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.
- Overrides:
sizeIfKnown
in classByteSource
-
read
public byte[] read()Description copied from class:ByteSource
Reads the full contents of this byte source as a byte array.- Overrides:
read
in classByteSource
-
read
Description copied from class:ByteSource
Reads the contents of this byte source using the givenprocessor
to process bytes as they are read. Stops when all bytes have been read or the consumer returnsfalse
. Returns the result produced by the processor.- Overrides:
read
in classByteSource
- Throws:
IOException
- if an I/O error occurs while reading from this source or ifprocessor
throws anIOException
-
copyTo
Description copied from class:ByteSource
Copies the contents of this byte source to the givenOutputStream
. Does not closeoutput
.- Overrides:
copyTo
in classByteSource
- Returns:
- the number of bytes copied
- Throws:
IOException
- if an I/O error occurs while reading from this source or writing tooutput
-
hash
Description copied from class:ByteSource
Hashes the contents of this byte source using the given hash function.- Overrides:
hash
in classByteSource
- Throws:
IOException
- if an I/O error occurs while reading from this source
-
slice
Description copied from class:ByteSource
Returns a view of a slice of this byte source that is at mostlength
bytes long starting at the givenoffset
. Ifoffset
is greater than the size of this source, the returned source will be empty. Ifoffset + length
is greater than the size of this source, the returned source will contain the slice starting atoffset
and ending at the end of this source.- Overrides:
slice
in classByteSource
-
toString
-