Top Description Inners Constructors Methods
com.google.common.io

public abstract Class ByteSource

extends Object
Class Inheritance
Known Direct Subclasses
com.google.common.io.ByteSource.SlicedByteSource, com.google.common.io.ByteSource.ByteArrayByteSource, com.google.common.io.ByteSource.ConcatenatedByteSource, com.google.common.io.CharSource.AsByteSource, com.google.common.io.Files.FileByteSource, com.google.common.io.MoreFiles.PathByteSource, com.google.common.io.Resources.UrlByteSource
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.io.ByteStreams.createBuffer, .ByteStreams.skipUpTo

A readable source of bytes, such as a file. Unlike an 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:

Note: In general, ByteSource is intended to be used for "file-like" sources that provide streams that are:

Author
Colin Decker
Since
14.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
ByteSource.AsCharSource

A char source that reads bytes from this source and decodes them as characters using a charset.

private static class
private static class
private static class
private class
ByteSource.SlicedByteSource

A view of a subsection of the containing byte source.

Constructor Summary

AccessConstructor and Description
protected
ByteSource()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public CharSource
asCharSource(Charset charset)

Returns a CharSource view of this byte source that decodes bytes read from this source as characters using the given Charset.

public static ByteSource

Returns:

a ByteSource containing the concatenated data
concat
(Iterable<? extends ByteSource>
the sources to concatenate
sources
)

Concatenates multiple ByteSource instances into a single source.

public static ByteSource

Returns:

a ByteSource containing the concatenated data
concat
(Iterator<? extends ByteSource>
the sources to concatenate
sources
)

Concatenates multiple ByteSource instances into a single source.

public static ByteSource

Returns:

a ByteSource containing the concatenated data
concat
(ByteSource...
the sources to concatenate
sources
)

Concatenates multiple ByteSource instances into a single source.

public boolean
contentEquals(ByteSource other)

Checks that the contents of this byte source are equal to the contents of the given byte source.

public long

Returns:

the number of bytes copied
copyTo
(OutputStream output)

Copies the contents of this byte source to the given OutputStream.

public long

Returns:

the number of bytes copied
copyTo
(ByteSink sink)

Copies the contents of this byte source to the given ByteSink.

private long
countBySkipping(InputStream in)

Counts the bytes in the given input stream using skip if possible.

public static ByteSource
empty()

Returns an immutable ByteSource that contains no bytes.

public HashCode
hash(HashFunction hashFunction)

Hashes the contents of this byte source using the given hash function.

public boolean
isEmpty()

Returns whether the source has zero bytes.

public InputStream
openBufferedStream()

Opens a new buffered InputStream for reading from this source.

public abstract InputStream
openStream()

Opens a new InputStream for reading from this source.

public byte[]
read()

Reads the full contents of this byte source as a byte array.

public <T extends @Nullable Object> T
read(ByteProcessor<T> processor)

Reads the contents of this byte source using the given processor to process bytes as they are read.

public long
size()

Returns the size of this source in bytes, even if doing so requires opening and traversing an entire stream.

public Optional<Long>
sizeIfKnown()

Returns the size of this source in bytes, if the size can be easily determined without actually opening the data stream.

public 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.

public static ByteSource
wrap(byte[] b)

Returns a view of the given byte array as a ByteSource.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait