Class ByteSource.SlicedByteSource
- java.lang.Object
-
- com.google.common.io.ByteSource
-
- com.google.common.io.ByteSource.SlicedByteSource
-
- Enclosing class:
- ByteSource
private final class ByteSource.SlicedByteSource extends ByteSource
A view of a subsection of the containing byte source.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.common.io.ByteSource
ByteSource.AsCharSource
-
-
Constructor Summary
Constructors Constructor Description SlicedByteSource(long offset, long length)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanisEmpty()Returns whether the source has zero bytes.java.io.InputStreamopenBufferedStream()Opens a new bufferedInputStreamfor reading from this source.java.io.InputStreamopenStream()Opens a newInputStreamfor 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.ByteSourceslice(long offset, long length)Returns a view of a slice of this byte source that is at mostlengthbytes long starting at the givenoffset.private java.io.InputStreamsliceStream(java.io.InputStream in)java.lang.StringtoString()-
Methods inherited from class com.google.common.io.ByteSource
asCharSource, concat, concat, concat, contentEquals, copyTo, copyTo, empty, hash, read, read, size, wrap
-
-
-
-
Method Detail
-
openStream
public java.io.InputStream openStream() throws java.io.IOExceptionDescription copied from class:ByteSourceOpens 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.
- Specified by:
openStreamin classByteSource- Throws:
java.io.IOException- if an I/O error occurs while opening the stream
-
openBufferedStream
public java.io.InputStream openBufferedStream() throws java.io.IOExceptionDescription copied from class:ByteSourceOpens a new bufferedInputStreamfor reading from this source. The returned stream is not required to be aBufferedInputStreamin 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:
openBufferedStreamin classByteSource- Throws:
java.io.IOException- if an I/O error occurs while opening the stream
-
sliceStream
private java.io.InputStream sliceStream(java.io.InputStream in) throws java.io.IOException- Throws:
java.io.IOException
-
slice
public ByteSource slice(long offset, long length)
Description copied from class:ByteSourceReturns 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.- Overrides:
slicein classByteSource
-
isEmpty
public boolean isEmpty() throws java.io.IOExceptionDescription copied from class:ByteSourceReturns 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
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.- Overrides:
isEmptyin classByteSource- Throws:
java.io.IOException- if an I/O error occurs
-
sizeIfKnown
public Optional<java.lang.Long> sizeIfKnown()
Description copied from class:ByteSourceReturns 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:
sizeIfKnownin classByteSource
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-