private final class ByteSource.SlicedByteSource extends ByteSource
Modifier and Type | Field and Description |
---|---|
(package private) long |
length |
(package private) long |
offset |
Constructor and Description |
---|
SlicedByteSource(long offset,
long length) |
Modifier and Type | Method and Description |
---|---|
boolean |
isEmpty()
Returns whether the source has zero bytes.
|
java.io.InputStream |
openBufferedStream()
Opens a new buffered
InputStream for reading from this source. |
java.io.InputStream |
openStream()
Opens a new
InputStream for reading from this source. |
Optional<java.lang.Long> |
sizeIfKnown()
Returns the size of this source in bytes, if the size can be easily determined without actually
opening the data stream.
|
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 . |
private java.io.InputStream |
sliceStream(java.io.InputStream in) |
java.lang.String |
toString() |
asCharSource, concat, concat, concat, contentEquals, copyTo, copyTo, empty, hash, read, read, size, wrap
public java.io.InputStream openStream() throws java.io.IOException
ByteSource
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.
openStream
in class ByteSource
java.io.IOException
- if an I/O error occurs in the process of opening the streampublic java.io.InputStream openBufferedStream() throws java.io.IOException
ByteSource
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 ByteSource.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.
openBufferedStream
in class ByteSource
java.io.IOException
- if an I/O error occurs in the process of opening the streamprivate java.io.InputStream sliceStream(java.io.InputStream in) throws java.io.IOException
java.io.IOException
public ByteSource slice(long offset, long length)
ByteSource
length
bytes long
starting at the given offset
. If offset
is greater than the size of this
source, the returned source will be empty. If offset + length
is greater than the size
of this source, the returned source will contain the slice starting at offset
and
ending at the end of this source.slice
in class ByteSource
public boolean isEmpty() throws java.io.IOException
ByteSource
ByteSource.sizeIfKnown()
returns zero, falling back to opening a stream and checking for EOF if the
size is not known.
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 return true
from isEmpty()
despite having readable content.
isEmpty
in class ByteSource
java.io.IOException
- if an I/O error occurspublic Optional<java.lang.Long> sizeIfKnown()
ByteSource
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.
sizeIfKnown
in class ByteSource
public java.lang.String toString()
toString
in class java.lang.Object