Class ArrayUtil

java.lang.Object
org.omegazero.common.util.ArrayUtil

public final class ArrayUtil extends Object
Utility functions for arrays.
Since:
2.2
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    byteArrayIndexOf(byte[] arr, byte[] seq)
    Deprecated.
    Since 2.7, use the equivalent method indexOf(byte[], byte[]) instead
    static int
    byteArrayIndexOf(byte[] arr, byte[] seq, int start)
    Deprecated.
    Since 2.7, use the equivalent method indexOf(byte[], byte[], int) instead
    static void
    checkBounds(boolean[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static void
    checkBounds(byte[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static void
    checkBounds(char[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static void
    checkBounds(double[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static void
    checkBounds(float[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static void
    checkBounds(int[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static void
    checkBounds(long[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static void
    checkBounds(short[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static <T> void
    checkBounds(T[] array, int offset, int length)
    Checks whether offset and length specify a valid set of elements in the array.
    static int
    indexOf(byte[] array, byte b)
    Searches the given byte b in the given array.
    static int
    indexOf(byte[] array, byte[] sequence)
    Searches for the sequence of bytes in the larger byte array.
    static int
    indexOf(byte[] array, byte[] sequence, int offset)
    Searches for the sequence of bytes in the larger byte array, starting at offset.
    static int
    indexOf(byte[] array, byte b, int offset, int length)
    Searches the given byte b in the given array, starting at offset and continuing for a maximum of length elements.
    static int
    indexOf(char[] array, char c)
    Searches the given char c in the given array.
    static int
    indexOf(char[] array, char c, int offset, int length)
    Searches the given char c in the given array, starting at offset and continuing for a maximum of length elements.
    static int
    indexOf(double[] array, double d)
    Searches the given double d in the given array.
    static int
    indexOf(double[] array, double d, int offset, int length)
    Searches the given double d in the given array, starting at offset and continuing for a maximum of length elements.
    static int
    indexOf(float[] array, float f)
    Searches the given float f in the given array.
    static int
    indexOf(float[] array, float f, int offset, int length)
    Searches the given float f in the given array, starting at offset and continuing for a maximum of length elements.
    static int
    indexOf(int[] array, int i)
    Searches the given int i in the given array.
    static int
    indexOf(int[] array, int i, int offset, int length)
    Searches the given int i in the given array, starting at offset and continuing for a maximum of length elements.
    static int
    indexOf(long[] array, long l)
    Searches the given long l in the given array.
    static int
    indexOf(long[] array, long l, int offset, int length)
    Searches the given long l in the given array, starting at offset and continuing for a maximum of length elements.
    static int
    indexOf(short[] array, short s)
    Searches the given short s in the given array.
    static int
    indexOf(short[] array, short s, int offset, int length)
    Searches the given short s in the given array, starting at offset and continuing for a maximum of length elements.
    static <T> int
    indexOf(T[] array, T e)
    Searches the given element e in the given array.
    static <T> int
    indexOf(T[] array, T e, int offset, int length)
    Searches the given element e in the given array, starting at offset and continuing for a maximum of length elements.
    static byte[]
    Reads all remaining data from the input stream (is) until end-of-file is reached, the VM runs out of memory or the maximum array size is reached.
    static byte[]
    Reads all remaining data from the input stream (is) until end-of-file is reached or the amount of bytes read exceeds the limit.
    static String
    toHexString(byte[] data)
    Represents the given data as a string of hexadecimal characters.
    static String
    toHexString(byte[] data, int offset, int length)
    Represents the given data, starting at offset and with the given length, as a string of hexadecimal characters.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • byteArrayIndexOf

      @Deprecated public static int byteArrayIndexOf(byte[] arr, byte[] seq)
      Deprecated.
      Since 2.7, use the equivalent method indexOf(byte[], byte[]) instead
      Searches for the sequence of bytes given in the seq in the larger arr byte array.
      Parameters:
      arr - The array in which to search for seq
      seq - The sequence of bytes to search in the larger arr array
      Returns:
      The index at which the given sequence starts in the arr array, or -1 if the sequence was not found
    • byteArrayIndexOf

      @Deprecated public static int byteArrayIndexOf(byte[] arr, byte[] seq, int start)
      Deprecated.
      Since 2.7, use the equivalent method indexOf(byte[], byte[], int) instead
      Searches for the sequence of bytes given in the seq in the larger arr byte array.
      Parameters:
      arr - The array in which to search for seq
      seq - The sequence of bytes to search in the larger arr array
      start - The position to start searching for seq
      Returns:
      The index at which the given sequence starts in the arr array, or -1 if the sequence was not found
    • indexOf

      public static int indexOf(byte[] array, byte[] sequence)
      Searches for the sequence of bytes in the larger byte array.

      A call to this method is equivalent to a call to

       indexOf(array, sequence, 0)
       
      Parameters:
      array - The array in which to search for the sequence
      sequence - The sequence of bytes to search in the larger array array
      Returns:
      The index at which the given sequence starts in the array, or -1 if the sequence was not found
      Since:
      2.7
      See Also:
      API Note:
      This method existed before version 2.7, but was called "byteArrayIndexOf"
    • indexOf

      public static int indexOf(byte[] array, byte[] sequence, int offset)
      Searches for the sequence of bytes in the larger byte array, starting at offset.
      Parameters:
      array - The array in which to search for the sequence
      sequence - The sequence of bytes to search in the larger array array
      offset - The index to start at
      Returns:
      The index at which the given sequence starts in the array, or -1 if the sequence was not found
      Since:
      2.7
      See Also:
      API Note:
      This method existed before version 2.7, but was called "byteArrayIndexOf"
    • indexOf

      public static int indexOf(byte[] array, byte b)
      Searches the given byte b in the given array.

      A call to this method is equivalent to a call to

       indexOf(array, b, 0, array.length)
       
      Parameters:
      array - The array to search in
      b - The byte value to search in the array
      Returns:
      The index of the found byte, or -1 if it was not found
      Since:
      2.7
    • indexOf

      public static int indexOf(byte[] array, byte b, int offset, int length)
      Searches the given byte b in the given array, starting at offset and continuing for a maximum of length elements.

      If the byte is not found within the given bounds, -1 is returned.

      Parameters:
      array - The array to search in
      b - The byte value to search in the array
      offset - The index to start at
      length - The maximum number of elements to search
      Returns:
      The index of the found byte, or -1 if it was not found
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index of the search would exceed the length of the array
      Since:
      2.7
      See Also:
    • indexOf

      public static int indexOf(char[] array, char c)
      Searches the given char c in the given array.

      A call to this method is equivalent to a call to

       indexOf(array, b, 0, array.length)
       
      Parameters:
      array - The array to search in
      c - The char value to search in the array
      Returns:
      The index of the found char, or -1 if it was not found
      Since:
      2.8
    • indexOf

      public static int indexOf(char[] array, char c, int offset, int length)
      Searches the given char c in the given array, starting at offset and continuing for a maximum of length elements.

      If the char is not found within the given bounds, -1 is returned.

      Parameters:
      array - The array to search in
      c - The char value to search in the array
      offset - The index to start at
      length - The maximum number of elements to search
      Returns:
      The index of the found char, or -1 if it was not found
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index of the search would exceed the length of the array
      Since:
      2.8
      See Also:
    • indexOf

      public static int indexOf(short[] array, short s)
      Searches the given short s in the given array.

      A call to this method is equivalent to a call to

       indexOf(array, b, 0, array.length)
       
      Parameters:
      array - The array to search in
      s - The short value to search in the array
      Returns:
      The index of the found short, or -1 if it was not found
      Since:
      2.8
    • indexOf

      public static int indexOf(short[] array, short s, int offset, int length)
      Searches the given short s in the given array, starting at offset and continuing for a maximum of length elements.

      If the short is not found within the given bounds, -1 is returned.

      Parameters:
      array - The array to search in
      s - The short value to search in the array
      offset - The index to start at
      length - The maximum number of elements to search
      Returns:
      The index of the found short, or -1 if it was not found
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index of the search would exceed the length of the array
      Since:
      2.8
      See Also:
    • indexOf

      public static int indexOf(int[] array, int i)
      Searches the given int i in the given array.

      A call to this method is equivalent to a call to

       indexOf(array, b, 0, array.length)
       
      Parameters:
      array - The array to search in
      i - The int value to search in the array
      Returns:
      The index of the found int, or -1 if it was not found
      Since:
      2.8
    • indexOf

      public static int indexOf(int[] array, int i, int offset, int length)
      Searches the given int i in the given array, starting at offset and continuing for a maximum of length elements.

      If the int is not found within the given bounds, -1 is returned.

      Parameters:
      array - The array to search in
      i - The int value to search in the array
      offset - The index to start at
      length - The maximum number of elements to search
      Returns:
      The index of the found int, or -1 if it was not found
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index of the search would exceed the length of the array
      Since:
      2.8
      See Also:
    • indexOf

      public static int indexOf(long[] array, long l)
      Searches the given long l in the given array.

      A call to this method is equivalent to a call to

       indexOf(array, b, 0, array.length)
       
      Parameters:
      array - The array to search in
      l - The long value to search in the array
      Returns:
      The index of the found long, or -1 if it was not found
      Since:
      2.8
    • indexOf

      public static int indexOf(long[] array, long l, int offset, int length)
      Searches the given long l in the given array, starting at offset and continuing for a maximum of length elements.

      If the long is not found within the given bounds, -1 is returned.

      Parameters:
      array - The array to search in
      l - The long value to search in the array
      offset - The index to start at
      length - The maximum number of elements to search
      Returns:
      The index of the found long, or -1 if it was not found
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index of the search would exceed the length of the array
      Since:
      2.8
      See Also:
    • indexOf

      public static int indexOf(float[] array, float f)
      Searches the given float f in the given array.

      A call to this method is equivalent to a call to

       indexOf(array, b, 0, array.length)
       
      Parameters:
      array - The array to search in
      f - The float value to search in the array
      Returns:
      The index of the found float, or -1 if it was not found
      Since:
      2.8
    • indexOf

      public static int indexOf(float[] array, float f, int offset, int length)
      Searches the given float f in the given array, starting at offset and continuing for a maximum of length elements.

      If the float is not found within the given bounds, -1 is returned.

      Parameters:
      array - The array to search in
      f - The float value to search in the array
      offset - The index to start at
      length - The maximum number of elements to search
      Returns:
      The index of the found float, or -1 if it was not found
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index of the search would exceed the length of the array
      Since:
      2.8
      See Also:
    • indexOf

      public static int indexOf(double[] array, double d)
      Searches the given double d in the given array.

      A call to this method is equivalent to a call to

       indexOf(array, b, 0, array.length)
       
      Parameters:
      array - The array to search in
      d - The double value to search in the array
      Returns:
      The index of the found double, or -1 if it was not found
      Since:
      2.8
    • indexOf

      public static int indexOf(double[] array, double d, int offset, int length)
      Searches the given double d in the given array, starting at offset and continuing for a maximum of length elements.

      If the double is not found within the given bounds, -1 is returned.

      Parameters:
      array - The array to search in
      d - The double value to search in the array
      offset - The index to start at
      length - The maximum number of elements to search
      Returns:
      The index of the found double, or -1 if it was not found
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index of the search would exceed the length of the array
      Since:
      2.8
      See Also:
    • indexOf

      public static <T> int indexOf(T[] array, T e)
      Searches the given element e in the given array.

      A call to this method is equivalent to a call to

       indexOf(array, b, 0, array.length)
       
      Type Parameters:
      T - The element type
      Parameters:
      array - The array to search in
      e - The value to search in the array
      Returns:
      The index of the found element, or -1 if it was not found
      Since:
      2.8
    • indexOf

      public static <T> int indexOf(T[] array, T e, int offset, int length)
      Searches the given element e in the given array, starting at offset and continuing for a maximum of length elements.

      Equality is determined using Objects.equals(Object, Object). If the element is not found within the given bounds, -1 is returned.

      Type Parameters:
      T - The element type
      Parameters:
      array - The array to search in
      e - The value to search in the array
      offset - The index to start at
      length - The maximum number of elements to search
      Returns:
      The index of the found element, or -1 if it was not found
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index of the search would exceed the length of the array
      Since:
      2.8
      See Also:
    • checkBounds

      public static void checkBounds(boolean[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • checkBounds

      public static void checkBounds(byte[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • checkBounds

      public static void checkBounds(char[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • checkBounds

      public static void checkBounds(short[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • checkBounds

      public static void checkBounds(int[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • checkBounds

      public static void checkBounds(long[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • checkBounds

      public static void checkBounds(float[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • checkBounds

      public static void checkBounds(double[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • checkBounds

      public static <T> void checkBounds(T[] array, int offset, int length)
      Checks whether offset and length specify a valid set of elements in the array.
      Type Parameters:
      T - The element type
      Parameters:
      array - The array
      offset - The start index
      length - The number of required valid indices starting at offset
      Throws:
      IndexOutOfBoundsException - If offset is negative or if the end index exceeds the length of the array
      Since:
      2.8
    • readInputStreamToByteArray

      public static byte[] readInputStreamToByteArray(InputStream is) throws IOException
      Reads all remaining data from the input stream (is) until end-of-file is reached, the VM runs out of memory or the maximum array size is reached. To limit the amount of bytes to read, use readInputStreamToByteArray(InputStream, int) instead.
      Parameters:
      is - The input stream
      Returns:
      The byte array containing all data read from the input stream
      Throws:
      IOException - If an IO error occurs
      Since:
      2.3
    • readInputStreamToByteArray

      public static byte[] readInputStreamToByteArray(InputStream is, int limit) throws IOException
      Reads all remaining data from the input stream (is) until end-of-file is reached or the amount of bytes read exceeds the limit.
      Parameters:
      is - The input stream
      limit - The maximum amount of bytes to read before the read operation is canceled. May be 0 read any amount of bytes
      Returns:
      The byte array containing all data read from the input stream
      Throws:
      IOException - If an IO error occurs
      IndexOutOfBoundsException - If the amount of bytes read exceeds limit
      Since:
      2.3
    • toHexString

      public static String toHexString(byte[] data)
      Represents the given data as a string of hexadecimal characters.

      A call to this method is equivalent to a call to

      
                      toHexString(data, 0, data.length)
       
      Returns:
      The hexadecimal representation of the data
      Since:
      2.11.0
    • toHexString

      public static String toHexString(byte[] data, int offset, int length)
      Represents the given data, starting at offset and with the given length, as a string of hexadecimal characters.

      Each byte in the byte array slice is represented by exactly two lowercase hexadecimal characters.

      Returns:
      The hexadecimal representation of the data
      Throws:
      IndexOutOfBoundsException - If offset and length refer to an index outside of the given array
      Since:
      2.11.0