Class ArrayUtil
- Since:
- 2.2
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
byteArrayIndexOf
(byte[] arr, byte[] seq) Deprecated.static int
byteArrayIndexOf
(byte[] arr, byte[] seq, int start) Deprecated.Since 2.7, use the equivalent methodindexOf(byte[], byte[], int)
insteadstatic 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 givenbyte
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 givenbyte
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 givenchar
c in the given array.static int
indexOf
(char[] array, char c, int offset, int length) Searches the givenchar
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 givendouble
d in the given array.static int
indexOf
(double[] array, double d, int offset, int length) Searches the givendouble
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 givenfloat
f in the given array.static int
indexOf
(float[] array, float f, int offset, int length) Searches the givenfloat
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 givenint
i in the given array.static int
indexOf
(int[] array, int i, int offset, int length) Searches the givenint
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 givenlong
l in the given array.static int
indexOf
(long[] array, long l, int offset, int length) Searches the givenlong
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 givenshort
s in the given array.static int
indexOf
(short[] array, short s, int offset, int length) Searches the givenshort
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[]
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 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.
-
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
-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 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
-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 givenbyte
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 inb
- Thebyte
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 givenbyte
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 inb
- Thebyte
value 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-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 givenchar
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 inc
- Thechar
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 givenchar
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 inc
- Thechar
value 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-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 givenshort
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 ins
- Theshort
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 givenshort
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 ins
- Theshort
value 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-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 givenint
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 ini
- Theint
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 givenint
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 ini
- Theint
value 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-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 givenlong
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 inl
- Thelong
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 givenlong
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 inl
- Thelong
value 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-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 givenfloat
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 inf
- Thefloat
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 givenfloat
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 inf
- Thefloat
value 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-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 givendouble
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 ind
- Thedouble
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 givendouble
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 ind
- Thedouble
value 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-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 ine
- 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 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
-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 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