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 ) ) ; } }
Throws an appropriate exception if the given range is not included in the given array interval .