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
IOExceptionto 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 ArrayByteSourceEMPTYAn empty source. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description StringCharSourceasCharSource(Charset charset)StringCharSourceasCharSourceUtf8()Returns aCharSourcefor the same bytes, converted to UTF-8.StringCharSourceasCharSourceUtf8UsingBom()Returns aCharSourcefor the File, converted to UTF-8 using a Byte-Order Mark if available.booleancontentEquals(ByteSource other)static ArrayByteSourcecopyOf(byte[] array)Obtains an instance, copying the array.static ArrayByteSourcecopyOf(byte[] array, int fromIndex)Obtains an instance by copying part of an array.static ArrayByteSourcecopyOf(byte[] array, int fromIndexInclusive, int toIndexExclusive)Obtains an instance by copying part of an array.longcopyTo(OutputStream output)ArrayByteSourcedecode(ByteSourceCodec codec)Decodes the byte source.ArrayByteSourceencode(ByteSourceCodec codec)Encodes the byte source.booleanequals(Object obj)static ArrayByteSourcefrom(ByteSource other)Obtains an instance from another byte source.static ArrayByteSourcefrom(CheckedSupplier<? extends InputStream> inputStreamSupplier)Obtains an instance from an input stream.static ArrayByteSourcefrom(InputStream inputStream)Obtains an instance from an input stream.static ArrayByteSourcefrom(InputStream inputStream, int expectedSize)Obtains an instance from an input stream, specifying the expected size.static ArrayByteSourcefromBase64(String base64)Obtains an instance from a base-64 encoded string.static ArrayByteSourcefromHex(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.HashCodehash(HashFunction hashFunction)inthashCode()booleanisEmpty()Checks if the byte source is empty, throwing an unchecked exception.ArrayByteSourceload()Loads the content of the byte source into memory.org.joda.beans.MetaBeanmetaBean()static ArrayByteSourceofUnsafe(byte[] array)Obtains an instance, not copying the array.static ArrayByteSourceofUtf8(String str)Obtains an instance from a string using UTF-8.ByteArrayInputStreamopenBufferedStream()ByteArrayInputStreamopenStream()byte[]read()Reads the source as a byte array, throwing an unchecked exception.<T> Tread(ByteProcessor<T> processor)byte[]readUnsafe()Returns the underlying array.StringreadUtf8()Reads the source, converting to UTF-8.StringreadUtf8UsingBom()Reads the source, converting to UTF-8 using a Byte-Order Mark if available.longsize()Gets the size of the byte source, throwing an unchecked exception.Optional<Long>sizeIfKnown()Gets the size, which is always known.ArrayByteSourceslice(long offset, long length)ArrayByteSourcetoBase64()Encodes the byte source using base-64.StringtoBase64String()Encodes the byte source using base-64, returning a string.StringtoHexString()Encodes the byte source using hex, sometimes referred to as base-16, returning a string.ArrayByteSourcetoMd5()Deprecated.ArrayByteSourcetoSha512()Deprecated.StringtoString()ArrayByteSourcewithFileName(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:
 metaBeanin interfaceorg.joda.beans.Bean
 
- 
getFileName
public Optional<String> getFileName()
Description copied from class:BeanByteSourceGets the file name of the source.Most sources originate from a file-based location. This is captured and returned here where available.
- Overrides:
 getFileNamein 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:
 readUtf8in 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:
 readUtf8UsingBomin classBeanByteSource- Returns:
 - the UTF-8 string
 
 
- 
asCharSourceUtf8
public StringCharSource asCharSourceUtf8()
Description copied from class:BeanByteSourceReturns aCharSourcefor the same bytes, converted to UTF-8.- Overrides:
 asCharSourceUtf8in classBeanByteSource- Returns:
 - the equivalent 
CharSource 
 
- 
asCharSource
public StringCharSource asCharSource(Charset charset)
- Overrides:
 asCharSourcein classBeanByteSource
 
- 
asCharSourceUtf8UsingBom
public StringCharSource asCharSourceUtf8UsingBom()
Description copied from class:BeanByteSourceReturns aCharSourcefor the File, converted to UTF-8 using a Byte-Order Mark if available.- Overrides:
 asCharSourceUtf8UsingBomin classBeanByteSource- Returns:
 - the equivalent 
CharSource 
 
- 
hash
public HashCode hash(HashFunction hashFunction)
- Overrides:
 hashin 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:
 toBase64in 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:
 toBase64Stringin 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:
 openStreamin classByteSource
 
- 
openBufferedStream
public ByteArrayInputStream openBufferedStream()
- Overrides:
 openBufferedStreamin classByteSource
 
- 
isEmpty
public boolean isEmpty()
Description copied from class:BeanByteSourceChecks if the byte source is empty, throwing an unchecked exception.This overrides
ByteSourceto throwUncheckedIOExceptioninstead ofIOException.- Overrides:
 isEmptyin classBeanByteSource
 
- 
sizeIfKnown
public Optional<Long> sizeIfKnown()
Gets the size, which is always known.- Overrides:
 sizeIfKnownin classByteSource- Returns:
 - the size, which is always known
 
 
- 
size
public long size()
Description copied from class:BeanByteSourceGets the size of the byte source, throwing an unchecked exception.This overrides
ByteSourceto throwUncheckedIOExceptioninstead ofIOException.- Overrides:
 sizein classBeanByteSource
 
- 
slice
public ArrayByteSource slice(long offset, long length)
- Overrides:
 slicein classByteSource
 
- 
copyTo
public long copyTo(OutputStream output) throws IOException
- Overrides:
 copyToin classByteSource- Throws:
 IOException
 
- 
read
public byte[] read()
Description copied from class:BeanByteSourceReads the source as a byte array, throwing an unchecked exception.This overrides
ByteSourceto throwUncheckedIOExceptioninstead ofIOException.- Overrides:
 readin classBeanByteSource- Returns:
 - the byte array
 
 
- 
read
public <T> T read(ByteProcessor<T> processor) throws IOException
- Overrides:
 readin classByteSource- Throws:
 IOException
 
- 
contentEquals
public boolean contentEquals(ByteSource other) throws IOException
- Overrides:
 contentEqualsin classByteSource- Throws:
 IOException
 
- 
load
public ArrayByteSource load()
Description copied from class:BeanByteSourceLoads the content of the byte source into memory.- Overrides:
 loadin classBeanByteSource- Returns:
 - the byte array
 
 
 - 
 
 -