Package: primitives

## [Java] Class byte[]

- primitives.byte[]

-     public class byte[]
      extends Object

  GDK enhancements for byte[].

  displayName:
    byte[]
  receiverFqcn:
    byte[]

- - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public boolean` | `any(Closure<?> predicate)` Iterates over the contents of a byte Array, and checks whether a predicate is valid for at least one element.
    `` | `public boolean` | `asBoolean()` Coerces a byte array to a boolean value.
    `` | `public BigDecimal` | `average()` Calculates the average of the bytes in the array.
    `` | `public List<List<Byte>>` | `chop(int chopSizes)` Chops the byte array into pieces, returning lists with sizes corresponding to the supplied chop sizes.
    `` | `public boolean` | `contains(Object value)` Checks whether the array contains the given value.
    `` | `public Number` | `count(Object value)` Counts the number of occurrences of the given value inside this array.
    `` | `public String` | `digest(String algorithm)` digest the byte array
    `` | `public byte[]` | `each(Consumer<Byte> consumer)` Iterates through a byte[] passing each byte to the given consumer.
    `` | `public void` | `eachByte(Closure<?> closure)` Traverses through each byte of this byte array.
    `` | `public byte[]` | `eachWithIndex(Closure<?> closure)` Iterates through a byte[], passing each byte and the element's index (a counter starting at zero) to the given closure.
    `` | `public Writable` | `encodeBase64(boolean chunked)` Produce a Writable object which writes the Base64 encoding of the byte array.
    `` | `public Writable` | `encodeBase64()` Produce a Writable object which writes the Base64 encoding of the byte array.
    `` | `public Writable` | `encodeBase64Url()` Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array.
    `` | `public Writable` | `encodeBase64Url(boolean pad)` Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array.
    `` | `public Writable` | `encodeHex()` Produces a Writable that writes the hex encoding of the byte[].
    `` | `public boolean` | `equals(byte[] right)` Compares the contents of this array to the contents of the given array.
    `` | `public boolean` | `every(Closure<?> predicate)` Iterates over the contents of a byte Array, and checks whether a predicate is valid for all elements.
    `` | `public byte` | `first()` Returns the first item from the byte array.
    `` | `public List<Byte>` | `flatten()` Flattens an array.
    `` | `public List<Byte>` | `getAt(Range<?> range)` Supports the subscript operator for a byte array with a range giving the desired indices.
    `` | `public List<Byte>` | `getAt(IntRange range)` Supports the subscript operator for a byte array with an IntRange giving the desired indices.
    `` | `public List<Byte>` | `getAt(ObjectRange range)` Supports the subscript operator for a byte array with an ObjectRange giving the desired indices.
    `` | `public List<Byte>` | `getAt(Collection<?> indices)` Supports the subscript operator for a byte array with a (potentially nested) collection giving the desired indices.
    `` | `public IntRange` | `getIndices()` Returns indices of the byte array.
    `` | `public String` | `groovyToString()` Returns Groovy's list-like string representation for a byte array.
    `` | `public byte` | `head()` Returns the first item from the byte array.
    `` | `public byte[]` | `init()` Returns the items from the byte array excluding the last item.
    `` | `public String` | `join()` Concatenates the string representation of each item in this array.
    `` | `public String` | `join(String separator)` Concatenates the string representation of each item in this array, with the given String as a separator between each item.
    `` | `public byte` | `last()` Returns the last item from the byte array.
    `` | `public String` | `md5()` Calculate md5 of the byte array
    `` | `public void` | `putAt(IntRange range, byte[] source)` A helper method to allow arrays to be set with subscript operators.
    `` | `public void` | `putAt(IntRange range, Iterable<Number> source)` A helper method to allow arrays to be set with subscript operators.
    `` | `public byte[]` | `reverse()` Creates a new byte array containing items which are the same as this array but in reverse order.
    `` | `public byte[]` | `reverse(boolean mutate)` Reverses the items in an array.
    `` | `public byte[]` | `reverseEach(Closure<?> closure)` Iterates through a byte[] in reverse order passing each byte to the given closure.
    `` | `public String` | `sha256()` Calculate SHA-256 of the byte array
    `` | `public int` | `size()` Provides arrays with a `size` method similar to collections.
    `` | `public Stream<Byte>` | `stream()` Returns a sequential Stream with the specified array as its source.
    `` | `public byte` | `sum()` Sums the items in an array.
    `` | `public byte` | `sum(byte initialValue)` Sums the items in an array, adding the result to some initial value.
    `` | `public byte[]` | `swap(int i, int j)` Swaps two elements at the specified positions.
    `` | `public byte[]` | `tail()` Returns the items from the byte array excluding the first item.
    `` | `public List<Byte>` | `toList()` Converts this array to a List of the same size, with each element added to the list.
    `` | `public Set<Byte>` | `toSet()` Converts this array to a Set, with each unique element added to the set.
    `` | `public String` | `toString()` Returns the string representation of the given array.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `addShutdownHook, any, any, asBoolean, asType, collect, collect, collect, dump, each, eachMatch, eachMatch, eachWithIndex, every, every, find, find, findAll, findAll, findIndexOf, findIndexOf, findIndexValues, findIndexValues, findLastIndexOf, findLastIndexOf, findResult, findResult, findResult, findResult, getAt, getMetaClass, getMetaPropertyValues, getProperties, grep, grep, hasProperty, identity, inject, inject, inspect, invokeMethod, is, isCase, isNotCase, iterator, metaClass, print, print, printf, printf, println, println, println, putAt, respondsTo, respondsTo, setMetaClass, sleep, sleep, split, sprintf, sprintf, stream, tap, toString, use, use, use, with, with, withCloseable, withCloseable, withMethodClosure, withStream, withStream, withTraits`

- - ### Method Detail

    - #### public boolean any(Closure<?> predicate)

      Iterates over the contents of a byte Array, and checks whether a predicate is valid for at least one element.

           byte[] array = [0, 1, 2]
           assert array.any{ it > 1 }
           assert !array.any{ it > 3 }

      Parameters:
        `predicate` - the closure predicate used for matching
      Returns:
        true if any iteration for the bytes matches the closure predicate
      Since:
        5.0.0

    - #### public boolean asBoolean()

      Coerces a byte array to a boolean value. A byte array is false if the array is null or of length 0, and true otherwise.

           byte[] array1 = []
           assert !array1
           byte[] array2 = [0]
           assert array2

      Returns:
        the array's boolean value
      Since:
        1.7.4

    - #### public BigDecimal average()

      Calculates the average of the bytes in the array.

          assert 5.0G == ([2,4,6,8] as byte[]).average()

      Returns:
        The average of the items
      Since:
        3.0.0

    - #### public List<List<Byte>> chop(int chopSizes)

      Chops the byte array into pieces, returning lists with sizes corresponding to the supplied chop sizes. If the array isn't large enough, truncated (possibly empty) pieces are returned. Using a chop size of -1 will cause that piece to contain all remaining items from the array.

           byte[] array = [0, 1, 2]
           assert array.chop(1, 2) == [[0], [1, 2]]

      Parameters:
        `chopSizes` - the sizes for the returned pieces
      Returns:
        a list of lists chopping the original array elements into pieces determined by chopSizes
      See Also:
        ArrayGroovyMethods#collate(Object[], int)#collate(Object[], int)
      Since:
        5.0.0

    - #### public boolean contains(Object value)

      Checks whether the array contains the given value.

      Parameters:
        `value` - the value being searched for
      Returns:
        true if the array contains the value
      Since:
        1.8.6

    - #### public Number count(Object value)

      Counts the number of occurrences of the given value inside this array. Comparison is done using Groovy's == operator (using `compareTo(value) == 0`).

           byte[] array = [10, 20, 20, 30]
           assert array.count(20) == 2

      Parameters:
        `value` - the value being searched for
      Returns:
        the number of occurrences
      See Also:
        DefaultGroovyMethods#count(Iterator, Object)#count(Iterator, Object)
      Since:
        1.6.4

    - #### public String digest(String algorithm)

      digest the byte array

      throws:
        NoSuchAlgorithmException if the algorithm not found
      Returns:
        digested value
      Since:
        2.5.0
      See Also:
        MessageDigest#getInstance(java.lang.String)

    - #### public byte[] each(Consumer<Byte> consumer)

      Iterates through a byte[] passing each byte to the given consumer.

           byte[] array = [0, 1, 2]
           String result = ''
           array.each{ result += it }
           assert result == '012'

      Parameters:
        `consumer` - the consumer for each byte
      Returns:
        the self array
      Since:
        5.0.0

    - #### public void eachByte(Closure<?> closure)

      Traverses through each byte of this byte array.

      Parameters:
        `closure` - a closure
      Since:
        1.5.5

    - #### public byte[] eachWithIndex(Closure<?> closure)

      Iterates through a byte[], passing each byte and the element's index (a counter starting at zero) to the given closure.

           byte[] array = [10, 20, 30]
           String result = ''
           array.eachWithIndex{ item, index -> result += "$index($item)" }
           assert result == '0(10)1(20)2(30)'

      Parameters:
        `closure` - a Closure to operate on each byte
      Returns:
        the self array
      Since:
        5.0.0

    - #### public Writable encodeBase64(boolean chunked)

      Produce a Writable object which writes the Base64 encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 encoding and chunking see `RFC 4648`.

      Parameters:
        `chunked` - whether the Base64 encoded data should be MIME chunked
      Returns:
        object which will write the Base64 encoding of the byte array
      Since:
        1.5.7

    - #### public Writable encodeBase64()

      Produce a Writable object which writes the Base64 encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 encoding and chunking see `RFC 4648`.

      Returns:
        object which will write the Base64 encoding of the byte array
      Since:
        1.0

    - #### public Writable encodeBase64Url()

      Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 URL and Filename Safe encoding see `RFC 4648 - Section 5 Base 64 Encoding with URL and Filename Safe Alphabet`.

      The method omits padding and is equivalent to calling EncodingGroovyMethods.encodeBase64Url with a value of `false`.

      Returns:
        object which will write the Base64 URL and Filename Safe encoding of the byte array
      See Also:
        EncodingGroovyMethods.encodeBase64Url
      Since:
        2.5.0

    - #### public Writable encodeBase64Url(boolean pad)

      Produce a Writable object which writes the Base64 URL and Filename Safe encoding of the byte array. Calling toString() on the result returns the encoding as a String. For more information on Base64 URL and Filename Safe encoding see `RFC 4648 - Section 5 Base 64 Encoding with URL and Filename Safe Alphabet`.

      Parameters:
        `pad` - whether the encoded data should be padded
      Returns:
        object which will write the Base64 URL and Filename Safe encoding of the byte array
      Since:
        2.5.0

    - #### public Writable encodeHex()

      Produces a Writable that writes the hex encoding of the byte[]. Calling toString() on this Writable returns the hex encoding as a String. The hex encoding includes two characters for each byte and all letters are lower case.

      Returns:
        object which will write the hex encoding of the byte array
      See Also:
        Integer.toHexString

    - #### public boolean equals(byte[] right)

      Compares the contents of this array to the contents of the given array.

      Example usage:

           byte[] array1 = [4, 8]
           byte[] array2 = [4, 8]
           assert array1 !== array2
           assert array1.equals(array2)

      Parameters:
        `right` - the array being compared
      Returns:
        true if the contents of both arrays are equal.
      Since:
        5.0.0

    - #### public boolean every(Closure<?> predicate)

      Iterates over the contents of a byte Array, and checks whether a predicate is valid for all elements.

           byte[] array = [0, 1, 2]
           assert array.every{ it < 3 }
           assert !array.every{ it > 1 }

      Parameters:
        `predicate` - the closure predicate used for matching
      Returns:
        true if the predicate returns true for all elements in the array
      Since:
        5.0.0

    - #### public byte first()

      Returns the first item from the byte array.

           byte[] bytes = [1, 2, 3]
           assert bytes.first() == 1

      An alias for `head()`.
      throws:
        NoSuchElementException if the array is empty
      Returns:
        the first element
      Since:
        5.0.0

    - #### public List<Byte> flatten()

      Flattens an array. This array is added to a new collection. It is an alias for `toList()` but allows algorithms to be written which also work on multidimensional arrays or non-arrays where flattening would be applicable.

           byte[] array = [0, 1]
           assert array.flatten() == [0, 1]

      Returns:
        a Collection of the array elements
      Since:
        1.6.0

    - #### public List<Byte> getAt(Range<?> range)

      Supports the subscript operator for a byte array with a range giving the desired indices.

           byte[] array = [1, 3, 5, 7, 9, 11]
           assert array[2..<2] == [] // EmptyRange
           assert array[(0..5.5).step(2)] == [1, 5, 9]   // NumberRange
           assert array[(1..5.5).step(2)] == [3, 7, 11]  // NumberRange

      Parameters:
        `range` - a range indicating the indices for the items to retrieve
      Returns:
        list of the retrieved bytes
      Since:
        1.0

    - #### public List<Byte> getAt(IntRange range)

      Supports the subscript operator for a byte array with an IntRange giving the desired indices.

           byte[] array = [0, 10, 20, 30, 40]
           assert array[2..3] == [20, 30]
           assert array[-2..-1] == [30, 40]
           assert array[-1..-2] == [40, 30]

      Parameters:
        `range` - an IntRange indicating the indices for the items to retrieve
      Returns:
        list of the retrieved bytes
      Since:
        1.0

    - #### public List<Byte> getAt(ObjectRange range)

      Supports the subscript operator for a byte array with an ObjectRange giving the desired indices.

           byte[] array = [0, 10, 20, 30, 40]
           def range = new ObjectRange(2, 3)
           assert array[range] == [20, 30]

      Parameters:
        `range` - an ObjectRange indicating the indices for the items to retrieve
      Returns:
        list of the retrieved bytes
      Since:
        1.0

    - #### public List<Byte> getAt(Collection<?> indices)

      Supports the subscript operator for a byte array with a (potentially nested) collection giving the desired indices.

           byte[] array = [0, 2, 4, 6, 8]
           assert array[2, 3] == [4, 6]
           assert array[1, 0..1, [0, [-1]]] == [2, 0, 2, 0, 8]

      Parameters:
        `indices` - a collection of indices for the items to retrieve
      Returns:
        list of the bytes at the given indices
      Since:
        1.0

    - #### public IntRange getIndices()

      Returns indices of the byte array.

           byte[] array = [0, 1]
           assert array.indices == 0..1

      See Also:
        ArrayGroovyMethods#getIndices(Object[])#getIndices(Object[])
      Since:
        3.0.8

    - #### public String groovyToString()

      Returns Groovy's list-like string representation for a byte array.

      If disabled, e.g. via `-Dgroovy.extension.disable=groovyToString(byte[])`, the JDK's default array `toString()` is used instead.

      Returns:
        the string representation
      Since:
        6.0.0

    - #### public byte head()

      Returns the first item from the byte array.

           byte[] bytes = [1, 2, 3]
           assert bytes.head() == 1

      An alias for `first()`.
      throws:
        NoSuchElementException if the array is empty
      Returns:
        the first element
      Since:
        5.0.0

    - #### public byte[] init()

      Returns the items from the byte array excluding the last item.

           byte[] bytes = [1, 2, 3]
           def result = bytes.init()
           assert result == [1, 2]
           assert bytes.class.componentType == result.class.componentType

      throws:
        UnsupportedOperationException if the array is empty
      Returns:
        an array without its last element
      Since:
        5.0.0

    - #### public String join()

      Concatenates the string representation of each item in this array.

      Returns:
        the joined String
      Since:
        5.0.0

    - #### public String join(String separator)

      Concatenates the string representation of each item in this array, with the given String as a separator between each item.

      Parameters:
        `separator` - a String separator
      Returns:
        the joined String
      Since:
        2.4.1

    - #### public byte last()

      Returns the last item from the byte array.

           byte[] bytes = [1, 2, 3]
           assert bytes.last() == 3

      throws:
        NoSuchElementException if the array is empty
      Returns:
        the last element
      Since:
        5.0.0

    - #### public String md5()

      Calculate md5 of the byte array

      throws:
        NoSuchAlgorithmException if MD5 algorithm not found
      Returns:
        md5 value
      Since:
        2.5.0

    - #### public void putAt(IntRange range, byte[] source)

      A helper method to allow arrays to be set with subscript operators.

           byte[] from = [1, 1, 1]
           byte[] to = [0, 0, 0, 0, 0]
           to[1..3] = from
           assert to == [0, 1, 1, 1, 0]

      throws:
        IndexOutOfBoundsException if the source doesn't have sufficient elements or the IntRange is too big for the self array
      Parameters:
        `range` - the subset of the array to set
        `source` - the source array from which new values will come
      Since:
        5.0.0

    - #### public void putAt(IntRange range, Iterable<Number> source)

      A helper method to allow arrays to be set with subscript operators. Zero will be used if the source iterable has insufficient elements.

           def from = [1, 1, 1]
           byte[] to = [0, 0, 0, 0, 0]
           to[1..3] = from
           assert to == [0, 1, 1, 1, 0]

      Parameters:
        `range` - the subset of the array to set
        `source` - the source array from which new values will come
      Since:
        5.0.0

    - #### public byte[] reverse()

      Creates a new byte array containing items which are the same as this array but in reverse order.

           byte[] array = 1..2
           assert array.reverse() == 2..1

      Returns:
        an array containing the reversed items
      See Also:
        reverse(byte[], boolean)
      Since:
        5.0.0

    - #### public byte[] reverse(boolean mutate)

      Reverses the items in an array. If mutate is true, the original array is modified in place and returned. Otherwise, a new array containing the reversed items is produced.

           byte[] array = 1..3
           def yarra = array.reverse(true)
           assert array == 3..1
           assert yarra == 3..1
           assert array === yarra
           yarra = array.reverse(false)
           assert array !== yarra
           assert array == 3..1
           assert yarra == 1..3

      Parameters:
        `mutate` - `true` if the array itself should be reversed in place, `false` if a new array should be created
      Returns:
        an array containing the reversed items
      Since:
        5.0.0

    - #### public byte[] reverseEach(Closure<?> closure)

      Iterates through a byte[] in reverse order passing each byte to the given closure.

           byte[] array = [0, 1, 2]
           String result = ''
           array.reverseEach{ result += it }
           assert result == '210'

      Parameters:
        `closure` - the closure applied on each byte
      Returns:
        the self array
      Since:
        5.0.0

    - #### public String sha256()

      Calculate SHA-256 of the byte array

      throws:
        NoSuchAlgorithmException if SHA-256 algorithm not found
      Returns:
        SHA-256 value
      Since:
        2.5.3

    - #### public int size()

      Provides arrays with a `size` method similar to collections.

      Returns:
        the length of the array
      Since:
        1.0

    - #### public Stream<Byte> stream()

      Returns a sequential Stream with the specified array as its source.

      Returns:
        a `Stream` for the array
      Since:
        2.5.0

    - #### public byte sum()

      Sums the items in an array.

          assert (1+2+3+4 as byte) == ([1,2,3,4] as byte[]).sum()

      Returns:
        The sum of all the items
      Since:
        2.4.2

    - #### public byte sum(byte initialValue)

      Sums the items in an array, adding the result to some initial value.

          assert (5+1+2+3+4 as byte) == ([1,2,3,4] as byte[]).sum(5 as byte)

      Parameters:
        `initialValue` - the items in the array will be summed to this initial value
      Returns:
        The sum of all the items.
      Since:
        2.4.2

    - #### public byte[] swap(int i, int j)

      Swaps two elements at the specified positions.

      Example:

           assert ([1, 3, 2, 4] as byte[]) == ([1, 2, 3, 4] as byte[]).swap(1, 2)

      Parameters:
        `i` - a position
        `j` - a position
      Returns:
        self
      Since:
        2.4.0

    - #### public byte[] tail()

      Returns the items from the byte array excluding the first item.

           byte[] bytes = [1, 2, 3]
           def result = bytes.tail()
           assert result == [2, 3]
           assert bytes.class.componentType == result.class.componentType

      throws:
        UnsupportedOperationException if the array is empty
      Returns:
        an array without its first element
      Since:
        5.0.0

    - #### public List<Byte> toList()

      Converts this array to a List of the same size, with each element added to the list.

      Returns:
        A list containing the contents of this array.
      Since:
        1.0

    - #### public Set<Byte> toSet()

      Converts this array to a Set, with each unique element added to the set.

           byte[] array = [1, 2, 3, 2, 1]
           Set expected = [1, 2, 3]
           assert array.toSet() == expected

      Returns:
        A set containing the unique contents of this array.
      Since:
        1.8.0

    - #### public String toString()

      Returns the string representation of the given array.

           byte[] array = [1, 2, 3, 2, 1]
           assert array.toString() == '[1, 2, 3, 2, 1]'

      Returns:
        the string representation
      Since:
        1.6.0