Class ArrayByteSource
- java.lang.Object
-
- com.google.common.io.ByteSource
-
- com.opengamma.strata.collect.io.BeanByteSource
-
- com.opengamma.strata.collect.io.ArrayByteSource
-
- All Implemented Interfaces:
Serializable
,org.joda.beans.Bean
,org.joda.beans.ImmutableBean
public final class ArrayByteSource extends BeanByteSource implements org.joda.beans.ImmutableBean, Serializable
A byte source implementation that explicitly wraps a byte array.This implementation allows
IOException
to be avoided in many cases, and to be able to create and retrieve the internal array unsafely.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static ArrayByteSource
EMPTY
An empty source.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description StringCharSource
asCharSource(Charset charset)
StringCharSource
asCharSourceUtf8()
Returns aCharSource
for the same bytes, converted to UTF-8.StringCharSource
asCharSourceUtf8UsingBom()
Returns aCharSource
for the File, converted to UTF-8 using a Byte-Order Mark if available.boolean
contentEquals(ByteSource other)
static ArrayByteSource
copyOf(byte[] array)
Obtains an instance, copying the array.static ArrayByteSource
copyOf(byte[] array, int fromIndex)
Obtains an instance by copying part of an array.static ArrayByteSource
copyOf(byte[] array, int fromIndexInclusive, int toIndexExclusive)
Obtains an instance by copying part of an array.long
copyTo(OutputStream output)
ArrayByteSource
decode(ByteSourceCodec codec)
Decodes the byte source.ArrayByteSource
encode(ByteSourceCodec codec)
Encodes the byte source.boolean
equals(Object obj)
static ArrayByteSource
from(ByteSource other)
Obtains an instance from another byte source.static ArrayByteSource
from(CheckedSupplier<? extends InputStream> inputStreamSupplier)
Obtains an instance from an input stream.static ArrayByteSource
from(InputStream inputStream)
Obtains an instance from an input stream.static ArrayByteSource
from(InputStream inputStream, int expectedSize)
Obtains an instance from an input stream, specifying the expected size.static ArrayByteSource
fromBase64(String base64)
Obtains an instance from a base-64 encoded string.static ArrayByteSource
fromHex(String hex)
Obtains an instance from a hex encoded string, sometimes referred to as base-16.Optional<String>
getFileName()
Gets the file name of the source.HashCode
hash(HashFunction hashFunction)
int
hashCode()
boolean
isEmpty()
Checks if the byte source is empty, throwing an unchecked exception.ArrayByteSource
load()
Loads the content of the byte source into memory.org.joda.beans.MetaBean
metaBean()
static ArrayByteSource
ofUnsafe(byte[] array)
Obtains an instance, not copying the array.static ArrayByteSource
ofUtf8(String str)
Obtains an instance from a string using UTF-8.ByteArrayInputStream
openBufferedStream()
ByteArrayInputStream
openStream()
byte[]
read()
Reads the source as a byte array, throwing an unchecked exception.<T> T
read(ByteProcessor<T> processor)
byte[]
readUnsafe()
Returns the underlying array.String
readUtf8()
Reads the source, converting to UTF-8.String
readUtf8UsingBom()
Reads the source, converting to UTF-8 using a Byte-Order Mark if available.long
size()
Gets the size of the byte source, throwing an unchecked exception.Optional<Long>
sizeIfKnown()
Gets the size, which is always known.ArrayByteSource
slice(long offset, long length)
ArrayByteSource
toBase64()
Encodes the byte source using base-64.String
toBase64String()
Encodes the byte source using base-64, returning a string.String
toHexString()
Encodes the byte source using hex, sometimes referred to as base-16, returning a string.ArrayByteSource
toMd5()
Deprecated.ArrayByteSource
toSha512()
Deprecated.String
toString()
ArrayByteSource
withFileName(String fileName)
Returns an instance with the file name updated.-
Methods inherited from class com.opengamma.strata.collect.io.BeanByteSource
getFileNameOrThrow, toHash, toHashString
-
Methods inherited from class com.google.common.io.ByteSource
concat, concat, concat, copyTo, empty, wrap
-
-
-
-
Field Detail
-
EMPTY
public static final ArrayByteSource EMPTY
An empty source.
-
-
Method Detail
-
copyOf
public static ArrayByteSource copyOf(byte[] array)
Obtains an instance, copying the array.- Parameters:
array
- the array, copied- Returns:
- the byte source
-
copyOf
public static ArrayByteSource copyOf(byte[] array, int fromIndex)
Obtains an instance by copying part of an array.The input array is copied and not mutated.
- Parameters:
array
- the array to copyfromIndex
- the offset from the start of the array- Returns:
- an array containing the specified values
- Throws:
IndexOutOfBoundsException
- if the index is invalid
-
copyOf
public static ArrayByteSource copyOf(byte[] array, int fromIndexInclusive, int toIndexExclusive)
Obtains an instance by copying part of an array.The input array is copied and not mutated.
- Parameters:
array
- the array to copyfromIndexInclusive
- the start index of the input array to copy fromtoIndexExclusive
- the end index of the input array to copy to- Returns:
- an array containing the specified values
- Throws:
IndexOutOfBoundsException
- if the index is invalid
-
ofUnsafe
public static ArrayByteSource ofUnsafe(byte[] array)
Obtains an instance, not copying the array.This method is inherently unsafe as it relies on good behavior by callers. Callers must never make any changes to the passed in array after calling this method. Doing so would violate the immutability of this class.
- Parameters:
array
- the array, not copied- Returns:
- the byte source
-
ofUtf8
public static ArrayByteSource ofUtf8(String str)
Obtains an instance from a string using UTF-8.- Parameters:
str
- the string to store using UTF-8- Returns:
- the byte source
-
from
public static ArrayByteSource from(ByteSource other)
Obtains an instance from another byte source.- Parameters:
other
- the other byte source- Returns:
- the byte source
- Throws:
UncheckedIOException
- if an IO error occurs
-
from
public static ArrayByteSource from(CheckedSupplier<? extends InputStream> inputStreamSupplier)
Obtains an instance from an input stream.This method use the supplier to open the input stream, extract the bytes and close the stream. It is intended that invoking the supplier opens the stream. It is not intended that an already open stream is supplied.
- Parameters:
inputStreamSupplier
- the supplier of the input stream- Returns:
- the byte source
- Throws:
UncheckedIOException
- if an IO error occurs
-
from
public static ArrayByteSource from(InputStream inputStream) throws IOException
Obtains an instance from an input stream.This method uses an already open input stream, extracting the bytes. The stream is not closed - that is the responsibility of the caller.
- Parameters:
inputStream
- the open input stream, which will not be closed- Returns:
- the byte source
- Throws:
IOException
- if an IO error occurs
-
from
public static ArrayByteSource from(InputStream inputStream, int expectedSize) throws IOException
Obtains an instance from an input stream, specifying the expected size.This method uses an already open input stream, extracting the bytes. The stream is not closed - that is the responsibility of the caller.
- Parameters:
inputStream
- the open input stream, which will not be closedexpectedSize
- the expected size of the input, not negative- Returns:
- the byte source
- Throws:
IOException
- if an IO error occurs
-
fromBase64
public static ArrayByteSource fromBase64(String base64)
Obtains an instance from a base-64 encoded string.- Parameters:
base64
- the base64 string to convert- Returns:
- the decoded byte source
- Throws:
IllegalArgumentException
- if the input is not Base64 encoded
-
fromHex
public static ArrayByteSource fromHex(String hex)
Obtains an instance from a hex encoded string, sometimes referred to as base-16.- Parameters:
hex
- the hex string to convert- Returns:
- the decoded byte source
- Throws:
IllegalArgumentException
- if the input is not hex encoded
-
metaBean
public org.joda.beans.MetaBean metaBean()
- Specified by:
metaBean
in interfaceorg.joda.beans.Bean
-
getFileName
public Optional<String> getFileName()
Description copied from class:BeanByteSource
Gets the file name of the source.Most sources originate from a file-based location. This is captured and returned here where available.
- Overrides:
getFileName
in classBeanByteSource
- Returns:
- the file name, empty if not known
-
withFileName
public ArrayByteSource withFileName(String fileName)
Returns an instance with the file name updated.If a path is passed in, only the file name is retained.
- Parameters:
fileName
- the file name, an empty string can be used to remove the file name- Returns:
- a source with the specified file name
-
readUnsafe
public byte[] readUnsafe()
Returns the underlying array.This method is inherently unsafe as it relies on good behavior by callers. Callers must never make any changes to the array returned by this method. Doing so would violate the immutability of this class.
- Returns:
- the raw array
-
readUtf8
public String readUtf8()
Reads the source, converting to UTF-8.- Overrides:
readUtf8
in classBeanByteSource
- Returns:
- the UTF-8 string
-
readUtf8UsingBom
public String readUtf8UsingBom()
Reads the source, converting to UTF-8 using a Byte-Order Mark if available.- Overrides:
readUtf8UsingBom
in classBeanByteSource
- Returns:
- the UTF-8 string
-
asCharSourceUtf8
public StringCharSource asCharSourceUtf8()
Description copied from class:BeanByteSource
Returns aCharSource
for the same bytes, converted to UTF-8.- Overrides:
asCharSourceUtf8
in classBeanByteSource
- Returns:
- the equivalent
CharSource
-
asCharSource
public StringCharSource asCharSource(Charset charset)
- Overrides:
asCharSource
in classBeanByteSource
-
asCharSourceUtf8UsingBom
public StringCharSource asCharSourceUtf8UsingBom()
Description copied from class:BeanByteSource
Returns aCharSource
for the File, converted to UTF-8 using a Byte-Order Mark if available.- Overrides:
asCharSourceUtf8UsingBom
in classBeanByteSource
- Returns:
- the equivalent
CharSource
-
hash
public HashCode hash(HashFunction hashFunction)
- Overrides:
hash
in classBeanByteSource
-
toMd5
@Deprecated public ArrayByteSource toMd5()
Deprecated.Returns the MD5 hash of the bytes.The returned hash is in byte form.
- Returns:
- the MD5 hash
-
toSha512
@Deprecated public ArrayByteSource toSha512()
Deprecated.Returns the SHA-512 hash of the bytes.- Returns:
- the SHA-512 hash
-
toBase64
public ArrayByteSource toBase64()
Encodes the byte source using base-64.- Overrides:
toBase64
in classBeanByteSource
- Returns:
- the base-64 encoded form
-
toBase64String
public String toBase64String()
Encodes the byte source using base-64, returning a string.Equivalent to
toBase64().readUtf8()
.- Overrides:
toBase64String
in classBeanByteSource
- Returns:
- the base-64 encoded string
-
encode
public ArrayByteSource encode(ByteSourceCodec codec)
Encodes the byte source.- Parameters:
codec
- the codec to use- Returns:
- the encoded form
-
decode
public ArrayByteSource decode(ByteSourceCodec codec)
Decodes the byte source.- Parameters:
codec
- the codec to use- Returns:
- the decoded form
-
toHexString
public String toHexString()
Encodes the byte source using hex, sometimes referred to as base-16, returning a string.- Returns:
- the hex encoded string
-
openStream
public ByteArrayInputStream openStream()
- Specified by:
openStream
in classByteSource
-
openBufferedStream
public ByteArrayInputStream openBufferedStream()
- Overrides:
openBufferedStream
in classByteSource
-
isEmpty
public boolean isEmpty()
Description copied from class:BeanByteSource
Checks if the byte source is empty, throwing an unchecked exception.This overrides
ByteSource
to throwUncheckedIOException
instead ofIOException
.- Overrides:
isEmpty
in classBeanByteSource
-
sizeIfKnown
public Optional<Long> sizeIfKnown()
Gets the size, which is always known.- Overrides:
sizeIfKnown
in classByteSource
- Returns:
- the size, which is always known
-
size
public long size()
Description copied from class:BeanByteSource
Gets the size of the byte source, throwing an unchecked exception.This overrides
ByteSource
to throwUncheckedIOException
instead ofIOException
.- Overrides:
size
in classBeanByteSource
-
slice
public ArrayByteSource slice(long offset, long length)
- Overrides:
slice
in classByteSource
-
copyTo
public long copyTo(OutputStream output) throws IOException
- Overrides:
copyTo
in classByteSource
- Throws:
IOException
-
read
public byte[] read()
Description copied from class:BeanByteSource
Reads the source as a byte array, throwing an unchecked exception.This overrides
ByteSource
to throwUncheckedIOException
instead ofIOException
.- Overrides:
read
in classBeanByteSource
- Returns:
- the byte array
-
read
public <T> T read(ByteProcessor<T> processor) throws IOException
- Overrides:
read
in classByteSource
- Throws:
IOException
-
contentEquals
public boolean contentEquals(ByteSource other) throws IOException
- Overrides:
contentEquals
in classByteSource
- Throws:
IOException
-
load
public ArrayByteSource load()
Description copied from class:BeanByteSource
Loads the content of the byte source into memory.- Overrides:
load
in classBeanByteSource
- Returns:
- the byte array
-
-