code: static protected void checkOffsetLength(int bytesLength, int offset, int length)
              throws IllegalArgumentException {
          // offset cannot be negative
          if (offset < 0) {
              throw new IllegalArgumentException("The byte[] offset parameter cannot be negative");
          }
          // length cannot be negative either
          if (length < 0) {
              throw new IllegalArgumentException("The byte[] length parameter cannot be negative");
          }
          // is it a valid offset?  Must be < bytes.length if non-zero
          // if its zero, then the check below will validate if it would cause
          // a read past the length of the byte array
          if (offset != 0 && offset >= bytesLength) {
              throw new IllegalArgumentException("The byte[] offset (" + offset + ") must be < the length of the byte[] length (" + bytesLength + ")");
          }
          if (offset+length > bytesLength) {
              throw new IllegalArgumentException("The offset+length (" + (offset+length) + ") would read past the end of the byte[] (length=" + bytesLength + ")");
          }
      }
docs: Helper method for validating if an offset and length are valid for a given
  byte array. Checks if the offset or length is negative or if the offset+length
  would cause a read past the end of the byte array.
  @param bytesLength The length of the byte array to validate against
  @param offset The offset within the byte array
  @param length The length to read starting from the offset
  @throws java.lang.IllegalArgumentException If any of the above conditions
  are violated.