Class ArrayUtil
- Since:
- 2.2
-
Method Summary
Modifier and TypeMethodDescriptionstatic intbyteArrayIndexOf(byte[] arr, byte[] seq) Deprecated.static intbyteArrayIndexOf(byte[] arr, byte[] seq, int start) Deprecated.Since 2.7, use the equivalent methodindexOf(byte[], byte[], int)insteadstatic voidcheckBounds(boolean[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static voidcheckBounds(byte[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static voidcheckBounds(char[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static voidcheckBounds(double[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static voidcheckBounds(float[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static voidcheckBounds(int[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static voidcheckBounds(long[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static voidcheckBounds(short[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static <T> voidcheckBounds(T[] array, int offset, int length) Checks whether offset and length specify a valid set of elements in the array.static intindexOf(byte[] array, byte b) Searches the givenbyteb in the given array.static intindexOf(byte[] array, byte[] sequence) Searches for the sequence of bytes in the larger byte array.static intindexOf(byte[] array, byte[] sequence, int offset) Searches for the sequence of bytes in the larger byte array, starting at offset.static intindexOf(byte[] array, byte b, int offset, int length) Searches the givenbyteb in the given array, starting at offset and continuing for a maximum of length elements.static intindexOf(char[] array, char c) Searches the givencharc in the given array.static intindexOf(char[] array, char c, int offset, int length) Searches the givencharc in the given array, starting at offset and continuing for a maximum of length elements.static intindexOf(double[] array, double d) Searches the givendoubled in the given array.static intindexOf(double[] array, double d, int offset, int length) Searches the givendoubled in the given array, starting at offset and continuing for a maximum of length elements.static intindexOf(float[] array, float f) Searches the givenfloatf in the given array.static intindexOf(float[] array, float f, int offset, int length) Searches the givenfloatf in the given array, starting at offset and continuing for a maximum of length elements.static intindexOf(int[] array, int i) Searches the giveninti in the given array.static intindexOf(int[] array, int i, int offset, int length) Searches the giveninti in the given array, starting at offset and continuing for a maximum of length elements.static intindexOf(long[] array, long l) Searches the givenlongl in the given array.static intindexOf(long[] array, long l, int offset, int length) Searches the givenlongl in the given array, starting at offset and continuing for a maximum of length elements.static intindexOf(short[] array, short s) Searches the givenshorts in the given array.static intindexOf(short[] array, short s, int offset, int length) Searches the givenshorts in the given array, starting at offset and continuing for a maximum of length elements.static <T> intindexOf(T[] array, T e) Searches the given element e in the given array.static <T> intindexOf(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[]readInputStreamToByteArray(InputStream is, int limit) 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 StringtoHexString(byte[] data) Represents the given data as a string of hexadecimal characters.static StringtoHexString(byte[] data, int offset, int length) Represents the given data, starting at offset and with the given length, as a string of hexadecimal characters.
-
Method Details
-
byteArrayIndexOf
Deprecated.Since 2.7, use the equivalent methodindexOf(byte[], byte[])insteadSearches for the sequence of bytes given in the seq in the larger arr byte array.- Parameters:
arr- The array in which to search for seqseq- 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.Since 2.7, use the equivalent methodindexOf(byte[], byte[], int)insteadSearches for the sequence of bytes given in the seq in the larger arr byte array.- Parameters:
arr- The array in which to search for seqseq- The sequence of bytes to search in the larger arr arraystart- 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 sequencesequence- The sequence of bytes to search in the larger array array- Returns:
- The index at which the given sequence starts in the array, or
-1if 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 sequencesequence- The sequence of bytes to search in the larger array arrayoffset- The index to start at- Returns:
- The index at which the given sequence starts in the array, or
-1if 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 givenbyteb 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 inb- Thebytevalue to search in the array- Returns:
- The index of the found
byte, or-1if it was not found - Since:
- 2.7
-
indexOf
public static int indexOf(byte[] array, byte b, int offset, int length) Searches the givenbyteb in the given array, starting at offset and continuing for a maximum of length elements.If the
byteis not found within the given bounds,-1is returned.- Parameters:
array- The array to search inb- Thebytevalue to search in the arrayoffset- The index to start atlength- The maximum number of elements to search- Returns:
- The index of the found
byte, or-1if 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 givencharc 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 inc- Thecharvalue to search in the array- Returns:
- The index of the found
char, or-1if it was not found - Since:
- 2.8
-
indexOf
public static int indexOf(char[] array, char c, int offset, int length) Searches the givencharc in the given array, starting at offset and continuing for a maximum of length elements.If the
charis not found within the given bounds,-1is returned.- Parameters:
array- The array to search inc- Thecharvalue to search in the arrayoffset- The index to start atlength- The maximum number of elements to search- Returns:
- The index of the found
char, or-1if 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 givenshorts 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 ins- Theshortvalue to search in the array- Returns:
- The index of the found
short, or-1if it was not found - Since:
- 2.8
-
indexOf
public static int indexOf(short[] array, short s, int offset, int length) Searches the givenshorts in the given array, starting at offset and continuing for a maximum of length elements.If the
shortis not found within the given bounds,-1is returned.- Parameters:
array- The array to search ins- Theshortvalue to search in the arrayoffset- The index to start atlength- The maximum number of elements to search- Returns:
- The index of the found
short, or-1if 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 giveninti 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 ini- Theintvalue to search in the array- Returns:
- The index of the found
int, or-1if it was not found - Since:
- 2.8
-
indexOf
public static int indexOf(int[] array, int i, int offset, int length) Searches the giveninti in the given array, starting at offset and continuing for a maximum of length elements.If the
intis not found within the given bounds,-1is returned.- Parameters:
array- The array to search ini- Theintvalue to search in the arrayoffset- The index to start atlength- The maximum number of elements to search- Returns:
- The index of the found
int, or-1if 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 givenlongl 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 inl- Thelongvalue to search in the array- Returns:
- The index of the found
long, or-1if it was not found - Since:
- 2.8
-
indexOf
public static int indexOf(long[] array, long l, int offset, int length) Searches the givenlongl in the given array, starting at offset and continuing for a maximum of length elements.If the
longis not found within the given bounds,-1is returned.- Parameters:
array- The array to search inl- Thelongvalue to search in the arrayoffset- The index to start atlength- The maximum number of elements to search- Returns:
- The index of the found
long, or-1if 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 givenfloatf 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 inf- Thefloatvalue to search in the array- Returns:
- The index of the found
float, or-1if it was not found - Since:
- 2.8
-
indexOf
public static int indexOf(float[] array, float f, int offset, int length) Searches the givenfloatf in the given array, starting at offset and continuing for a maximum of length elements.If the
floatis not found within the given bounds,-1is returned.- Parameters:
array- The array to search inf- Thefloatvalue to search in the arrayoffset- The index to start atlength- The maximum number of elements to search- Returns:
- The index of the found
float, or-1if 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 givendoubled 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 ind- Thedoublevalue to search in the array- Returns:
- The index of the found
double, or-1if it was not found - Since:
- 2.8
-
indexOf
public static int indexOf(double[] array, double d, int offset, int length) Searches the givendoubled in the given array, starting at offset and continuing for a maximum of length elements.If the
doubleis not found within the given bounds,-1is returned.- Parameters:
array- The array to search ind- Thedoublevalue to search in the arrayoffset- The index to start atlength- The maximum number of elements to search- Returns:
- The index of the found
double, or-1if 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 ine- The value to search in the array- Returns:
- The index of the found element, or
-1if 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,-1is returned.- Type Parameters:
T- The element type- Parameters:
array- The array to search ine- The value to search in the arrayoffset- The index to start atlength- The maximum number of elements to search- Returns:
- The index of the found element, or
-1if 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 arrayoffset- The start indexlength- 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 arrayoffset- The start indexlength- 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 arrayoffset- The start indexlength- 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 arrayoffset- The start indexlength- 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 arrayoffset- The start indexlength- 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 arrayoffset- The start indexlength- 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 arrayoffset- The start indexlength- 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 arrayoffset- The start indexlength- 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 arrayoffset- The start indexlength- 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
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, usereadInputStreamToByteArray(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
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 streamlimit- 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 occursIndexOutOfBoundsException- If the amount of bytes read exceeds limit- Since:
- 2.3
-
toHexString
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
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
-
indexOf(byte[], byte[])instead