Class ByteSource
- java.lang.Object
-
- com.google.common.io.ByteSource
-
- All Implemented Interfaces:
InputSupplier<java.io.InputStream>
@Deprecated(since="2022-12-01") public abstract class ByteSource extends java.lang.Object implements InputSupplier<java.io.InputStream>
Deprecated.The Google Guava Core Libraries are deprecated and will not be part of the AEM SDK after April 2023A readable source of bytes, such as a file. Unlike anInputStream
, aByteSource
is not an open, stateful stream for input that can be read and closed. Instead, it is an immutable supplier ofInputStream
instances.ByteSource
provides two kinds of methods:- Methods that return a stream: These methods should return a new, independent instance each time they are called. The caller is responsible for ensuring that the returned stream is closed.
- Convenience methods: These are implementations of common operations that are typically implemented by opening a stream using one of the methods in the first category, doing something and finally closing the stream that was opened.
- Since:
- 14.0
-
-
Constructor Summary
Constructors Constructor Description ByteSource()
Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description CharSource
asCharSource(java.nio.charset.Charset charset)
Deprecated.Returns aCharSource
view of this byte source that decodes bytes read from this source as characters using the givenCharset
.static ByteSource
concat(ByteSource... sources)
Deprecated.Concatenates multipleByteSource
instances into a single source.static ByteSource
concat(java.lang.Iterable<? extends ByteSource> sources)
Deprecated.Concatenates multipleByteSource
instances into a single source.static ByteSource
concat(java.util.Iterator<? extends ByteSource> sources)
Deprecated.Concatenates multipleByteSource
instances into a single source.boolean
contentEquals(ByteSource other)
Deprecated.Checks that the contents of this byte source are equal to the contents of the given byte source.long
copyTo(ByteSink sink)
Deprecated.Copies the contents of this byte source to the givenByteSink
.long
copyTo(java.io.OutputStream output)
Deprecated.Copies the contents of this byte source to the givenOutputStream
.static ByteSource
empty()
Deprecated.Returns an immutableByteSource
that contains no bytes.java.io.InputStream
getInput()
Deprecated.This method is only provided for temporary compatibility with theInputSupplier
interface and should not be called directly.HashCode
hash(HashFunction hashFunction)
Deprecated.Hashes the contents of this byte source using the given hash function.boolean
isEmpty()
Deprecated.Returns whether the source has zero bytes.java.io.InputStream
openBufferedStream()
Deprecated.Opens a new bufferedInputStream
for reading from this source.abstract java.io.InputStream
openStream()
Deprecated.Opens a newInputStream
for reading from this source.byte[]
read()
Deprecated.Reads the full contents of this byte source as a byte array.long
size()
Deprecated.Returns the size of this source in bytes.ByteSource
slice(long offset, long length)
Deprecated.Returns a view of a slice of this byte source that is at mostlength
bytes long starting at the givenoffset
.static ByteSource
wrap(byte[] b)
Deprecated.Returns a view of the given byte array as aByteSource
.
-
-
-
Method Detail
-
asCharSource
public CharSource asCharSource(java.nio.charset.Charset charset)
Deprecated.Returns aCharSource
view of this byte source that decodes bytes read from this source as characters using the givenCharset
.
-
openStream
public abstract java.io.InputStream openStream() throws java.io.IOException
Deprecated.Opens a newInputStream
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.
- Throws:
java.io.IOException
- if an I/O error occurs in the process of opening the stream
-
getInput
@Deprecated public final java.io.InputStream getInput() throws java.io.IOException
Deprecated.This method is only provided for temporary compatibility with theInputSupplier
interface and should not be called directly. UseopenStream()
instead.This method is a temporary method provided for easing migration from suppliers to sources and sinks.- Specified by:
getInput
in interfaceInputSupplier<java.io.InputStream>
- Throws:
java.io.IOException
- Since:
- 15.0
-
openBufferedStream
public java.io.InputStream openBufferedStream() throws java.io.IOException
Deprecated.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 toopenStream()
when the stream returned by that method does not benefit from additional buffering (for example, aByteArrayInputStream
). 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.
- Throws:
java.io.IOException
- if an I/O error occurs in the process of opening the stream- Since:
- 15.0 (in 14.0 with return type
BufferedInputStream
)
-
slice
public ByteSource slice(long offset, long length)
Deprecated.Returns a view of a slice of this byte source that is at mostlength
bytes long starting at the givenoffset
.- Throws:
java.lang.IllegalArgumentException
- ifoffset
orlength
is negative
-
isEmpty
public boolean isEmpty() throws java.io.IOException
Deprecated.Returns whether the source has zero bytes. The default implementation is to open a stream and check for EOF.- Throws:
java.io.IOException
- if an I/O error occurs- Since:
- 15.0
-
size
public long size() throws java.io.IOException
Deprecated.Returns the size of this source in bytes. For most implementations, this is 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.For some sources, such as a file, this method may use a more efficient implementation. 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).
In either case, if this is a mutable source such as a file, the size it returns may not be the same number of bytes a subsequent read would return.
- Throws:
java.io.IOException
- if an I/O error occurs in the process of reading the size of this source
-
copyTo
public long copyTo(java.io.OutputStream output) throws java.io.IOException
Deprecated.Copies the contents of this byte source to the givenOutputStream
. Does not closeoutput
.- Throws:
java.io.IOException
- if an I/O error occurs in the process of reading from this source or writing tooutput
-
copyTo
public long copyTo(ByteSink sink) throws java.io.IOException
Deprecated.Copies the contents of this byte source to the givenByteSink
.- Throws:
java.io.IOException
- if an I/O error occurs in the process of reading from this source or writing tosink
-
read
public byte[] read() throws java.io.IOException
Deprecated.Reads the full contents of this byte source as a byte array.- Throws:
java.io.IOException
- if an I/O error occurs in the process of reading from this source
-
hash
public HashCode hash(HashFunction hashFunction) throws java.io.IOException
Deprecated.Hashes the contents of this byte source using the given hash function.- Throws:
java.io.IOException
- if an I/O error occurs in the process of reading from this source
-
contentEquals
public boolean contentEquals(ByteSource other) throws java.io.IOException
Deprecated.Checks that the contents of this byte source are equal to the contents of the given byte source.- Throws:
java.io.IOException
- if an I/O error occurs in the process of reading from this source orother
-
concat
public static ByteSource concat(java.lang.Iterable<? extends ByteSource> sources)
Deprecated.Concatenates multipleByteSource
instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
- Parameters:
sources
- the sources to concatenate- Returns:
- a
ByteSource
containing the concatenated data - Since:
- 15.0
-
concat
public static ByteSource concat(java.util.Iterator<? extends ByteSource> sources)
Deprecated.Concatenates multipleByteSource
instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
Note: The input
Iterator
will be copied to anImmutableList
when this method is called. This will fail if the iterator is infinite and may cause problems if the iterator eagerly fetches data for each source when iterated (rather than producing sources that only load data through their streams). Prefer using theconcat(Iterable)
overload if possible.- Parameters:
sources
- the sources to concatenate- Returns:
- a
ByteSource
containing the concatenated data - Throws:
java.lang.NullPointerException
- if any ofsources
isnull
- Since:
- 15.0
-
concat
public static ByteSource concat(ByteSource... sources)
Deprecated.Concatenates multipleByteSource
instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
- Parameters:
sources
- the sources to concatenate- Returns:
- a
ByteSource
containing the concatenated data - Throws:
java.lang.NullPointerException
- if any ofsources
isnull
- Since:
- 15.0
-
wrap
public static ByteSource wrap(byte[] b)
Deprecated.Returns a view of the given byte array as aByteSource
. To view only a specific range in the array, useByteSource.wrap(b).slice(offset, length)
.- Since:
- 15.0 (since 14.0 as
ByteStreams.asByteSource(byte[])
).
-
empty
public static ByteSource empty()
Deprecated.Returns an immutableByteSource
that contains no bytes.- Since:
- 15.0
-
-