private static final class MoreFiles.PathByteSource extends ByteSource
ByteSource.AsCharSource
Modifier and Type | Field and Description |
---|---|
private static java.nio.file.LinkOption[] |
FOLLOW_LINKS |
private boolean |
followLinks |
private java.nio.file.OpenOption[] |
options |
private java.nio.file.Path |
path |
Modifier | Constructor and Description |
---|---|
private |
PathByteSource(java.nio.file.Path path,
java.nio.file.OpenOption... options) |
Modifier and Type | Method and Description |
---|---|
CharSource |
asCharSource(java.nio.charset.Charset charset)
Returns a
CharSource view of this byte source that decodes bytes read from this source
as characters using the given Charset . |
private static boolean |
followLinks(java.nio.file.OpenOption[] options) |
java.io.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.
|
private java.nio.file.attribute.BasicFileAttributes |
readAttributes() |
long |
size()
Returns the size of this source in bytes, even if doing so requires opening and traversing an
entire stream.
|
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.
|
java.lang.String |
toString() |
concat, concat, concat, contentEquals, copyTo, copyTo, empty, hash, isEmpty, openBufferedStream, read, slice, wrap
private static final java.nio.file.LinkOption[] FOLLOW_LINKS
private final java.nio.file.Path path
private final java.nio.file.OpenOption[] options
private final boolean followLinks
private PathByteSource(java.nio.file.Path path, java.nio.file.OpenOption... options)
private static boolean followLinks(java.nio.file.OpenOption[] options)
public java.io.InputStream openStream() throws java.io.IOException
ByteSource
InputStream
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.
openStream
in class ByteSource
java.io.IOException
- if an I/O error occurs while opening the streamprivate java.nio.file.attribute.BasicFileAttributes readAttributes() throws java.io.IOException
java.io.IOException
public 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 long size() throws java.io.IOException
ByteSource
ByteSource.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 (or skip
, 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.
size
in class ByteSource
java.io.IOException
- if an I/O error occurs while reading the size of this sourcepublic byte[] read() throws java.io.IOException
ByteSource
read
in class ByteSource
java.io.IOException
- if an I/O error occurs while reading from this sourcepublic CharSource asCharSource(java.nio.charset.Charset charset)
ByteSource
CharSource
view of this byte source that decodes bytes read from this source
as characters using the given Charset
.
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 original ByteSource
is
returned, rather than round-trip encoding. Subclasses that override this method should behave
the same way.
asCharSource
in class ByteSource
public java.lang.String toString()
toString
in class java.lang.Object