Class 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
    • Method Detail

      • copyOf

        public static ArrayByteSource copyOf​(byte[] array)
        Creates 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 copy
        fromIndex - 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 copy
        fromIndexInclusive - the start index of the input array to copy from
        toIndexExclusive - 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)
        Creates 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)
        Creates 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)
        Creates 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<InputStream> inputStreamSupplier)
        Creates 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
      • fromBase64

        public static ArrayByteSource fromBase64​(String base64)
        Creates 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)
        Creates 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 interface org.joda.beans.Bean
      • 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 class BeanByteSource
        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 class BeanByteSource
        Returns:
        the UTF-8 string
      • asCharSourceUtf8UsingBom

        public CharSource asCharSourceUtf8UsingBom()
        Returns a CharSource for the same bytes, converted to UTF-8 using a Byte-Order Mark if available.
        Overrides:
        asCharSourceUtf8UsingBom in class BeanByteSource
        Returns:
        the equivalent CharSource
      • toMd5

        public ArrayByteSource toMd5()
        Returns the MD5 hash of the bytes.
        Returns:
        the MD5 hash
      • toSha512

        public ArrayByteSource toSha512()
        Returns the SHA-512 hash of the bytes.
        Returns:
        the SHA-512 hash
      • toBase64

        public ArrayByteSource toBase64()
        Encodes the byte source using base-64.
        Returns:
        the base-64 encoded form
      • toBase64String

        public String toBase64String()
        Encodes the byte source using base-64, returning a string.

        Equivalent to toBase64().readUtf8().

        Returns:
        the base-64 encoded string
      • toHexString

        public String toHexString()
        Encodes the byte source using hex, sometimes referred to as base-16, returning a string.
        Returns:
        the hex encoded string
      • sizeIfKnown

        public Optional<Long> sizeIfKnown()
        Gets the size, which is always known.
        Overrides:
        sizeIfKnown in class ByteSource
        Returns:
        the size, which is always known
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object