I have found another Ticket in OpenJdk which is relevant to that change. As stated there
Bounds checking is not difficult to write explicitly but it can be
easy to make trivial mistakes, such as introducing overflow bugs. It
is advantageous to consolidate such checks from a correctness and
security/integrity perspective. Further more in certain cases it is an
opportunity to optimize, via an intrinsic, certain checks and guide
hotspot towards unsigned comparisons.
Enhancements to the Java platform will enable optimizations of loops
over bounds greater than the minimum and maximum range of int values,
requiring bounds checking operating on long values.
In the Foreign Memory Access API (JEP 393), the bounds of a memory
segments are expressed as long values. Since bound checks involving
longs are not currently optimized, the implementation of the foreign
memory access API had to resort to several tricks to gauge whether a
memory segment can be considered "small" (e.g. whose size fits in an
int value) and then use int operations on small segments, accordingly.
While in most cases, these workarounds are hidden inside the API
implementation, they add a significant cost in terms of complexity and
long term maintenance.
Solution Overload the existing int accepting bounds check methods
defined in java.util.Objects with long accepting bounds check methods.
The following static methods are added to java.util.Objects. The specification is identical to that of the existing int accepting bounds check methods of the same method name.
/**
* Checks if the {@code index} is within the bounds of the range from
* {@code 0} (inclusive) to {@code length} (exclusive).
*
* <p>The {@code index} is defined to be out of bounds if any of the
* following inequalities is true:
* <ul>
* <li>{@code index < 0}</li>
* <li>{@code index >= length}</li>
* <li>{@code length < 0}, which is implied from the former inequalities</li>
* </ul>
*
* @param index the index
* @param length the upper-bound (exclusive) of the range
* @return {@code index} if it is within bounds of the range
* @throws IndexOutOfBoundsException if the {@code index} is out of bounds
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* @since 16
*/
public static
long checkIndex(long index, long length)
/**
* Checks if the sub-range from {@code fromIndex} (inclusive) to
* {@code toIndex} (exclusive) is within the bounds of range from {@code 0}
* (inclusive) to {@code length} (exclusive).
*
* <p>The sub-range is defined to be out of bounds if any of the following
* inequalities is true:
* <ul>
* <li>{@code fromIndex < 0}</li>
* <li>{@code fromIndex > toIndex}</li>
* <li>{@code toIndex > length}</li>
* <li>{@code length < 0}, which is implied from the former inequalities</li>
* </ul>
*
* @param fromIndex the lower-bound (inclusive) of the sub-range
* @param toIndex the upper-bound (exclusive) of the sub-range
* @param length the upper-bound (exclusive) the range
* @return {@code fromIndex} if the sub-range within bounds of the range
* @throws IndexOutOfBoundsException if the sub-range is out of bounds
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* @since 16
*/
public static
long checkFromToIndex(long fromIndex, long toIndex, long length)
/**
* Checks if the sub-range from {@code fromIndex} (inclusive) to
* {@code fromIndex + size} (exclusive) is within the bounds of range from
* {@code 0} (inclusive) to {@code length} (exclusive).
*
* <p>The sub-range is defined to be out of bounds if any of the following
* inequalities is true:
* <ul>
* <li>{@code fromIndex < 0}</li>
* <li>{@code size < 0}</li>
* <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
* <li>{@code length < 0}, which is implied from the former inequalities</li>
* </ul>
*
* @param fromIndex the lower-bound (inclusive) of the sub-interval
* @param size the size of the sub-range
* @param length the upper-bound (exclusive) of the range
* @return {@code fromIndex} if the sub-range within bounds of the range
* @throws IndexOutOfBoundsException if the sub-range is out of bounds
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* @since 16
*/
public static
long checkFromIndexSize(long fromIndex, long size, long length)
The following constructor is added to java.lang.IndexOutOfBoundsException:
/**
* Constructs a new {@code IndexOutOfBoundsException} class with an
* argument indicating the illegal index.
*
* <p>The index is included in this exception's detail message. The
* exact presentation format of the detail message is unspecified.
*
* @param index the illegal index.
* @since 16
*/
public IndexOutOfBoundsException(long index)
Jira Issue: Add utility methods to check long indexes and ranges
new ArrayList<>().get(0)
? Are you confusingIndexOutOfBoundsException
withArrayIndexOutOfBoundsException
? – Claviclepublic E get(int index) { try { return listIterator(index).next(); } catch (NoSuchElementException exc) { throw new IndexOutOfBoundsException("Index: "+index); }
it is also handled as int – Styriaint
might be used. – Bizetget
variant with long to match you specific needs to that new subtype. Just because Javas standard classes don't have that out of the box means that you can't either. – ClavicleIndexOutOfBoundsException(int)
, would be confusing for the receiver of the exception. But I don't think that new constructor will be used for List, it is impractical with that many items. – ClavicleList
is misplaced (though we would eventually like to lift the 32 bit limits there too.) This was precipitated by Project Panama, which brings better native heap access to Java. The Foreign Memory API (a replacement for direct byte buffers) allows long-indexed heap access to native memory segments, motivating this change to IOOBE. – Helminthiasis