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.
      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)  
      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.
      com.google.common.hash.HashCode hash​(com.google.common.hash.HashFunction hashFunction)  
      boolean isEmpty()  
      static ArrayByteSource ofUnsafe​(byte[] array)
      Creates an instance, not copying the array.
      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.
      java.lang.String toString()  
      • Methods inherited from class com.google.common.io.ByteSource

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

        clone, equals, finalize, getClass, hashCode, 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
      • 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
      • 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
      • 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
      • 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
      • toString

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