public static void checkArrayRange(long startIndex, int length, long arrayLength, String startIndexArgName, String lengthArgName) throws ArrayIndexOutOfBoundsException, IllegalArgumentException {
        // Check for non-negative length.
        if (length < 0) {
            throw new IllegalArgumentException(badArgumentMessage(lengthArgName, "length must be a non-negative integer."));
        }

        // Check for valid start index.
        if (startIndex < 0 || startIndex >= arrayLength) {
            // The only valid case here is if the range has zero elements and the array bounds also has zero elements.
            if (!(startIndex == 0 && length == 0 && arrayLength == 0)) {
                throw new ArrayIndexOutOfBoundsException(badStartOffsetMessage(startIndex, arrayLength, startIndexArgName));
            }
        }

        // Check for valid end offset. Note that end offset can be equal to upBoundExclusive, because this is a range.
        if (startIndex + length > arrayLength) {
            throw new ArrayIndexOutOfBoundsException(badLengthMessage(startIndex, length, arrayLength, startIndexArgName, lengthArgName));
        }
    }
public static void checkArrayRange ( long startIndex , int length , long arrayLength , String startIndexArgName , String lengthArgName ) throws ArrayIndexOutOfBoundsException , IllegalArgumentException { if ( length < 0 ) { throw new IllegalArgumentException ( badArgumentMessage ( lengthArgName , "length must be a non-negative integer." ) ) ; } if ( startIndex < 0 || startIndex >= arrayLength ) { if ( ! ( startIndex == 0 && length == 0 && arrayLength == 0 ) ) { throw new ArrayIndexOutOfBoundsException ( badStartOffsetMessage ( startIndex , arrayLength , startIndexArgName ) ) ; } } if ( startIndex + length > arrayLength ) { throw new ArrayIndexOutOfBoundsException ( badLengthMessage ( startIndex , length , arrayLength , startIndexArgName , lengthArgName ) ) ; } }
Throws an appropriate exception if the given range is not included in the given array interval.

@param startIndex        The First index in the range.
@param length            The number of items in the range.
@param arrayLength       The length of the array.
@param startIndexArgName The name of the start index argument.
@param lengthArgName     The name of the length argument.
@throws ArrayIndexOutOfBoundsException If startIndex is less than lowBoundInclusive or if startIndex+length is
greater than upBoundExclusive.
@throws IllegalArgumentException       If length is a negative number.