Class ArrayByteSource


  • public final class ArrayByteSource
    extends com.google.common.io.ByteSource
    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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static ArrayByteSource EMPTY
      An empty source.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.google.common.io.CharSource asCharSourceUtf8UsingBom()
      Returns a CharSource for the same bytes, converted to UTF-8 using a Byte-Order Mark if available.
      boolean contentEquals​(com.google.common.io.ByteSource other)  
      static ArrayByteSource copyOf​(byte[] array)
      Creates 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​(java.io.OutputStream output)  
      boolean equals​(java.lang.Object obj)  
      static ArrayByteSource from​(com.google.common.io.ByteSource other)
      Creates an instance from another byte source.
      static ArrayByteSource from​(CheckedSupplier<java.io.InputStream> inputStreamSupplier)
      Creates an instance from an input stream.
      static ArrayByteSource fromBase64​(java.lang.String base64)
      Creates an instance from a base-64 encoded string.
      static ArrayByteSource fromHex​(java.lang.String hex)
      Creates an instance from a hex encoded string, sometimes referred to as base-16.
      com.google.common.hash.HashCode hash​(com.google.common.hash.HashFunction hashFunction)  
      int hashCode()  
      boolean isEmpty()  
      static ArrayByteSource ofUnsafe​(byte[] array)
      Creates an instance, not copying the array.
      static ArrayByteSource ofUtf8​(java.lang.String str)
      Creates an instance from a string using UTF-8.
      java.io.ByteArrayInputStream openBufferedStream()  
      java.io.ByteArrayInputStream openStream()  
      byte[] read()  
      <T> T read​(com.google.common.io.ByteProcessor<T> processor)  
      byte[] readUnsafe()
      Returns the underlying array.
      java.lang.String readUtf8()
      Reads the source, converting to UTF-8.
      java.lang.String readUtf8UsingBom()
      Reads the source, converting to UTF-8 using a Byte-Order Mark if available.
      long size()  
      com.google.common.base.Optional<java.lang.Long> sizeIfKnown()
      Gets the size, which is always known.
      ArrayByteSource slice​(long offset, long length)  
      ArrayByteSource toBase64()
      Encodes the byte source using base-64.
      java.lang.String toBase64String()
      Encodes the byte source using base-64, returning a string.
      java.lang.String toHexString()
      Encodes the byte source using hex, sometimes referred to as base-16, returning a string.
      ArrayByteSource toMd5()
      Returns the MD5 hash of the bytes.
      ArrayByteSource toSha512()
      Returns the SHA-512 hash of the bytes.
      java.lang.String toString()  
      • Methods inherited from class com.google.common.io.ByteSource

        asCharSource, concat, concat, concat, copyTo, empty, wrap
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • 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:
        java.lang.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:
        java.lang.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​(java.lang.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​(com.google.common.io.ByteSource other)
        Creates an instance from another byte source.
        Parameters:
        other - the other byte source
        Returns:
        the byte source
        Throws:
        java.io.UncheckedIOException - if an IO error occurs
      • from

        public static ArrayByteSource from​(CheckedSupplier<java.io.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:
        java.io.UncheckedIOException - if an IO error occurs
      • fromBase64

        public static ArrayByteSource fromBase64​(java.lang.String base64)
        Creates an instance from a base-64 encoded string.
        Parameters:
        base64 - the base64 string to convert
        Returns:
        the decoded byte source
        Throws:
        java.lang.IllegalArgumentException - if the input is not Base64 encoded
      • fromHex

        public static ArrayByteSource fromHex​(java.lang.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:
        java.lang.IllegalArgumentException - if the input is not hex encoded
      • 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 java.lang.String readUtf8()
        Reads the source, converting to UTF-8.
        Returns:
        the UTF-8 string
      • readUtf8UsingBom

        public java.lang.String readUtf8UsingBom()
        Reads the source, converting to UTF-8 using a Byte-Order Mark if available.
        Returns:
        the UTF-8 string
      • asCharSourceUtf8UsingBom

        public com.google.common.io.CharSource asCharSourceUtf8UsingBom()
        Returns a CharSource for the same bytes, converted to UTF-8 using a Byte-Order Mark if available.
        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 java.lang.String toBase64String()
        Encodes the byte source using base-64, returning a string.

        Equivalent to toBase64().readUtf8().

        Returns:
        the base-64 encoded string
      • toHexString

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

        public java.io.ByteArrayInputStream openStream()
        Specified by:
        openStream in class com.google.common.io.ByteSource
      • openBufferedStream

        public java.io.ByteArrayInputStream openBufferedStream()
        Overrides:
        openBufferedStream in class com.google.common.io.ByteSource
      • isEmpty

        public boolean isEmpty()
        Overrides:
        isEmpty in class com.google.common.io.ByteSource
      • sizeIfKnown

        public com.google.common.base.Optional<java.lang.Long> sizeIfKnown()
        Gets the size, which is always known.
        Overrides:
        sizeIfKnown in class com.google.common.io.ByteSource
        Returns:
        the size, which is always known
      • size

        public long size()
        Overrides:
        size in class com.google.common.io.ByteSource
      • slice

        public ArrayByteSource slice​(long offset,
                                     long length)
        Overrides:
        slice in class com.google.common.io.ByteSource
      • copyTo

        public long copyTo​(java.io.OutputStream output)
                    throws java.io.IOException
        Overrides:
        copyTo in class com.google.common.io.ByteSource
        Throws:
        java.io.IOException
      • read

        public byte[] read()
        Overrides:
        read in class com.google.common.io.ByteSource
      • read

        public <T> T read​(com.google.common.io.ByteProcessor<T> processor)
                   throws java.io.IOException
        Overrides:
        read in class com.google.common.io.ByteSource
        Throws:
        java.io.IOException
      • hash

        public com.google.common.hash.HashCode hash​(com.google.common.hash.HashFunction hashFunction)
        Overrides:
        hash in class com.google.common.io.ByteSource
      • contentEquals

        public boolean contentEquals​(com.google.common.io.ByteSource other)
                              throws java.io.IOException
        Overrides:
        contentEquals in class com.google.common.io.ByteSource
        Throws:
        java.io.IOException
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object