8268698: Use Objects.check{Index,FromToIndex,FromIndexSize} for java.base

Reviewed-by: mchung, rriggs
This commit is contained in:
Yi Yang 2021-07-13 02:23:16 +00:00
parent a4e5f08fef
commit afe957cd97
40 changed files with 186 additions and 284 deletions

@ -32,6 +32,7 @@ import java.util.Spliterator;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
import jdk.internal.util.Preconditions;
import static java.lang.String.COMPACT_STRINGS;
import static java.lang.String.UTF16;
@ -409,9 +410,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
*/
public int codePointBefore(int index) {
int i = index - 1;
if (i < 0 || i >= count) {
throw new StringIndexOutOfBoundsException(index);
}
checkIndex(i, count);
if (isLatin1()) {
return value[i] & 0xff;
}
@ -505,9 +504,9 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
*/
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
{
checkRangeSIOOBE(srcBegin, srcEnd, count); // compatible to old version
Preconditions.checkFromToIndex(srcBegin, srcEnd, count, Preconditions.SIOOBE_FORMATTER); // compatible to old version
int n = srcEnd - srcBegin;
checkRange(dstBegin, dstBegin + n, dst.length);
Preconditions.checkFromToIndex(dstBegin, dstBegin + n, dst.length, Preconditions.IOOBE_FORMATTER);
if (isLatin1()) {
StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin);
} else {
@ -677,7 +676,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
if (s == null) {
s = "null";
}
checkRange(start, end, s.length());
Preconditions.checkFromToIndex(start, end, s.length(), Preconditions.IOOBE_FORMATTER);
int len = end - start;
ensureCapacityInternal(count + len);
if (s instanceof String) {
@ -736,7 +735,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
*/
public AbstractStringBuilder append(char[] str, int offset, int len) {
int end = offset + len;
checkRange(offset, end, str.length);
Preconditions.checkFromToIndex(offset, end, str.length, Preconditions.IOOBE_FORMATTER);
ensureCapacityInternal(count + len);
appendChars(str, offset, end);
return this;
@ -914,7 +913,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
if (end > count) {
end = count;
}
checkRangeSIOOBE(start, end, count);
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
int len = end - start;
if (len > 0) {
shift(end, -len);
@ -997,7 +996,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
if (end > count) {
end = count;
}
checkRangeSIOOBE(start, end, count);
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
int len = str.length();
int newCount = count + len - (end - start);
ensureCapacityInternal(newCount);
@ -1067,7 +1066,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
* greater than {@code end}.
*/
public String substring(int start, int end) {
checkRangeSIOOBE(start, end, count);
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
if (isLatin1()) {
return StringLatin1.newString(value, start, end - start);
}
@ -1104,7 +1103,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
int len)
{
checkOffset(index, count);
checkRangeSIOOBE(offset, offset + len, str.length);
Preconditions.checkFromToIndex(offset, offset + len, str.length, Preconditions.SIOOBE_FORMATTER);
ensureCapacityInternal(count + len);
shift(index, len);
count += len;
@ -1292,7 +1291,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
s = "null";
}
checkOffset(dstOffset, count);
checkRange(start, end, s.length());
Preconditions.checkFromToIndex(start, end, s.length(), Preconditions.IOOBE_FORMATTER);
int len = end - start;
ensureCapacityInternal(count + len);
shift(dstOffset, len);
@ -1795,20 +1794,4 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
}
count += end - off;
}
/* IndexOutOfBoundsException, if out of bounds */
private static void checkRange(int start, int end, int len) {
if (start < 0 || start > end || end > len) {
throw new IndexOutOfBoundsException(
"start " + start + ", end " + end + ", length " + len);
}
}
/* StringIndexOutOfBoundsException, if out of bounds */
private static void checkRangeSIOOBE(int start, int end, int len) {
if (start < 0 || start > end || end > len) {
throw new StringIndexOutOfBoundsException(
"start " + start + ", end " + end + ", length " + len);
}
}
}

@ -34,6 +34,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
@ -9249,10 +9250,7 @@ class Character implements java.io.Serializable, Comparable<Character>, Constabl
* @since 1.5
*/
public static int codePointCount(CharSequence seq, int beginIndex, int endIndex) {
int length = seq.length();
if (beginIndex < 0 || endIndex > length || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromToIndex(beginIndex, endIndex, seq.length());
int n = endIndex - beginIndex;
for (int i = beginIndex; i < endIndex; ) {
if (isHighSurrogate(seq.charAt(i++)) && i < endIndex &&
@ -9284,9 +9282,7 @@ class Character implements java.io.Serializable, Comparable<Character>, Constabl
* @since 1.5
*/
public static int codePointCount(char[] a, int offset, int count) {
if (count > a.length - offset || offset < 0 || count < 0) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromIndexSize(count, offset, a.length);
return codePointCountImpl(a, offset, count);
}

@ -709,10 +709,8 @@ public final class Integer extends Number
public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
throws NumberFormatException {
Objects.requireNonNull(s);
Objects.checkFromToIndex(beginIndex, endIndex, s.length());
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
throw new IndexOutOfBoundsException();
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
@ -892,10 +890,8 @@ public final class Integer extends Number
public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)
throws NumberFormatException {
Objects.requireNonNull(s);
Objects.checkFromToIndex(beginIndex, endIndex, s.length());
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
throw new IndexOutOfBoundsException();
}
int start = beginIndex, len = endIndex - beginIndex;
if (len > 0) {

@ -752,10 +752,8 @@ public final class Long extends Number
public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix)
throws NumberFormatException {
Objects.requireNonNull(s);
Objects.checkFromToIndex(beginIndex, endIndex, s.length());
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
throw new IndexOutOfBoundsException();
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
@ -998,10 +996,8 @@ public final class Long extends Number
public static long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix)
throws NumberFormatException {
Objects.requireNonNull(s);
Objects.checkFromToIndex(beginIndex, endIndex, s.length());
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
throw new IndexOutOfBoundsException();
}
int start = beginIndex, len = endIndex - beginIndex;
if (len > 0) {

@ -51,6 +51,7 @@ import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
@ -1571,9 +1572,7 @@ public final class String
*/
public int codePointBefore(int index) {
int i = index - 1;
if (i < 0 || i >= length()) {
throw new StringIndexOutOfBoundsException(index);
}
checkIndex(i, length());
if (isLatin1()) {
return (value[i] & 0xff);
}
@ -1602,10 +1601,7 @@ public final class String
* @since 1.5
*/
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || beginIndex > endIndex ||
endIndex > length()) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromToIndex(beginIndex, endIndex, length());
if (isLatin1()) {
return endIndex - beginIndex;
}
@ -4556,10 +4552,7 @@ public final class String
* negative or greater than or equal to {@code length}.
*/
static void checkIndex(int index, int length) {
if (index < 0 || index >= length) {
throw new StringIndexOutOfBoundsException("index " + index +
", length " + length);
}
Preconditions.checkIndex(index, length, Preconditions.SIOOBE_FORMATTER);
}
/*
@ -4567,10 +4560,7 @@ public final class String
* is negative or greater than {@code length}.
*/
static void checkOffset(int offset, int length) {
if (offset < 0 || offset > length) {
throw new StringIndexOutOfBoundsException("offset " + offset +
", length " + length);
}
Preconditions.checkFromToIndex(offset, length, length, Preconditions.SIOOBE_FORMATTER);
}
/*
@ -4582,10 +4572,7 @@ public final class String
* or {@code offset} is greater than {@code length - count}
*/
static void checkBoundsOffCount(int offset, int count, int length) {
if (offset < 0 || count < 0 || offset > length - count) {
throw new StringIndexOutOfBoundsException(
"offset " + offset + ", count " + count + ", length " + length);
}
Preconditions.checkFromIndexSize(offset, count, length, Preconditions.SIOOBE_FORMATTER);
}
/*
@ -4597,10 +4584,7 @@ public final class String
* {@code end}, or {@code end} is greater than {@code length}.
*/
static void checkBoundsBeginEnd(int begin, int end, int length) {
if (begin < 0 || begin > end || end > length) {
throw new StringIndexOutOfBoundsException(
"begin " + begin + ", end " + end + ", length " + length);
}
Preconditions.checkFromToIndex(begin, end, length, Preconditions.SIOOBE_FORMATTER);
}
/**

@ -39,14 +39,13 @@ import jdk.internal.vm.annotation.IntrinsicCandidate;
import static java.lang.String.LATIN1;
import static java.lang.String.UTF16;
import static java.lang.String.checkIndex;
import static java.lang.String.checkOffset;
final class StringLatin1 {
public static char charAt(byte[] value, int index) {
if (index < 0 || index >= value.length) {
throw new StringIndexOutOfBoundsException(index);
}
checkIndex(index, value.length);
return (char)(value[index] & 0xff);
}

@ -30,6 +30,7 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import static java.util.Objects.requireNonNull;
@ -113,8 +114,9 @@ final class MethodTypeDescImpl implements MethodTypeDesc {
@Override
public MethodTypeDesc dropParameterTypes(int start, int end) {
if (start < 0 || start >= argTypes.length || end < 0 || end > argTypes.length || start > end)
throw new IndexOutOfBoundsException();
Objects.checkIndex(start, argTypes.length);
Objects.checkFromToIndex(start, end, argTypes.length);
ClassDesc[] newArgs = new ClassDesc[argTypes.length - (end - start)];
System.arraycopy(argTypes, 0, newArgs, 0, start);
System.arraycopy(argTypes, end, newArgs, start, argTypes.length - end);

@ -28,9 +28,6 @@ package java.lang.invoke;
import java.util.*;
import jdk.internal.vm.annotation.Stable;
import static java.lang.invoke.MethodHandleStatics.rangeCheck1;
import static java.lang.invoke.MethodHandleStatics.rangeCheck2;
/** Utility class for implementing ConstantGroup. */
/*non-public*/
abstract class AbstractConstantGroup implements ConstantGroup {
@ -119,11 +116,11 @@ abstract class AbstractConstantGroup implements ConstantGroup {
super(end - start);
this.self = self;
this.offset = start;
rangeCheck2(start, end, size);
Objects.checkFromToIndex(start, end, size);
}
private int mapIndex(int index) {
return rangeCheck1(index, size) + offset;
return Objects.checkIndex(index, size) + offset;
}
@Override
@ -143,7 +140,7 @@ abstract class AbstractConstantGroup implements ConstantGroup {
@Override
public ConstantGroup subGroup(int start, int end) {
rangeCheck2(start, end, size);
Objects.checkFromToIndex(start, end, size);
return new SubGroup(self, offset + start, offset + end);
}
@ -160,7 +157,7 @@ abstract class AbstractConstantGroup implements ConstantGroup {
@Override
public int copyConstants(int start, int end,
Object[] buf, int pos) throws LinkageError {
rangeCheck2(start, end, size);
Objects.checkFromToIndex(start, end, size);
return self.copyConstants(offset + start, offset + end,
buf, pos);
}
@ -169,7 +166,7 @@ abstract class AbstractConstantGroup implements ConstantGroup {
public int copyConstants(int start, int end,
Object[] buf, int pos,
Object ifNotPresent) {
rangeCheck2(start, end, size);
Objects.checkFromToIndex(start, end, size);
return self.copyConstants(offset + start, offset + end,
buf, pos, ifNotPresent);
}
@ -189,7 +186,7 @@ abstract class AbstractConstantGroup implements ConstantGroup {
this.offset = start;
this.resolving = resolving;
this.ifNotPresent = ifNotPresent;
rangeCheck2(start, end, self.size());
Objects.checkFromToIndex(start, end, self.size());
}
AsList(ConstantGroup self, int start, int end) {
this(self, start, end, true, null);
@ -200,7 +197,7 @@ abstract class AbstractConstantGroup implements ConstantGroup {
}
private int mapIndex(int index) {
return rangeCheck1(index, size) + offset;
return Objects.checkIndex(index, size) + offset;
}
@Override public final int size() {
@ -223,7 +220,7 @@ abstract class AbstractConstantGroup implements ConstantGroup {
}
@Override public List<Object> subList(int start, int end) {
rangeCheck2(start, end, size);
Objects.checkFromToIndex(start, end, size);
return new AsList(self, offset + start, offset + end,
resolving, ifNotPresent);
}

@ -189,15 +189,4 @@ class MethodHandleStatics {
if (obj != null || obj2 != null) message = message + ": " + obj + ", " + obj2;
return message;
}
/*non-public*/
static void rangeCheck2(int start, int end, int size) {
if (0 > start || start > end || end > size)
throw new IndexOutOfBoundsException(start+".."+end);
}
/*non-public*/
static int rangeCheck1(int index, int size) {
if (0 > index || index >= size)
throw new IndexOutOfBoundsException(index);
return index;
}
}

@ -2179,15 +2179,6 @@ public abstract class VarHandle implements Constable {
UNSAFE.fullFence();
}
static final BiFunction<String, List<Number>, ArrayIndexOutOfBoundsException>
AIOOBE_SUPPLIER = Preconditions.outOfBoundsExceptionFormatter(
new Function<String, ArrayIndexOutOfBoundsException>() {
@Override
public ArrayIndexOutOfBoundsException apply(String s) {
return new ArrayIndexOutOfBoundsException(s);
}
});
private static final long VFORM_OFFSET;
static {

@ -830,7 +830,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.get$Type$Volatile(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase);
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase);
}
@ForceInline
@ -842,7 +842,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
UNSAFE.put$Type$Volatile(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -855,7 +855,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.get$Type$Opaque(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase);
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase);
}
@ForceInline
@ -867,7 +867,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
UNSAFE.put$Type$Opaque(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -880,7 +880,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.get$Type$Acquire(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase);
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase);
}
@ForceInline
@ -892,7 +892,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
UNSAFE.put$Type$Release(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
#if[CAS]
@ -906,7 +906,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.compareAndSet$Type$(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?handle.componentType.cast(expected):expected},
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -920,7 +920,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.compareAndExchange$Type$(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?handle.componentType.cast(expected):expected},
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -934,7 +934,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.compareAndExchange$Type$Acquire(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?handle.componentType.cast(expected):expected},
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -948,7 +948,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.compareAndExchange$Type$Release(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?handle.componentType.cast(expected):expected},
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -962,7 +962,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.weakCompareAndSet$Type$Plain(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?handle.componentType.cast(expected):expected},
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -976,7 +976,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.weakCompareAndSet$Type$(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?handle.componentType.cast(expected):expected},
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -990,7 +990,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.weakCompareAndSet$Type$Acquire(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?handle.componentType.cast(expected):expected},
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -1004,7 +1004,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.weakCompareAndSet$Type$Release(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?handle.componentType.cast(expected):expected},
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -1018,7 +1018,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.getAndSet$Type$(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -1031,7 +1031,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.getAndSet$Type$Acquire(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
@ -1044,7 +1044,7 @@ final class VarHandle$Type$s {
$type$[] array = ($type$[]) oarray;
#end[Object]
return UNSAFE.getAndSet$Type$Release(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
{#if[Object]?runtimeTypeCheck(handle, array, value):value});
}
#end[CAS]
@ -1055,7 +1055,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndAdd$Type$(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1064,7 +1064,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndAdd$Type$Acquire(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1073,7 +1073,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndAdd$Type$Release(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
#end[AtomicAdd]
@ -1084,7 +1084,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseOr$Type$(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1093,7 +1093,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseOr$Type$Release(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1102,7 +1102,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseOr$Type$Acquire(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1111,7 +1111,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseAnd$Type$(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1120,7 +1120,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseAnd$Type$Release(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1129,7 +1129,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseAnd$Type$Acquire(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1138,7 +1138,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseXor$Type$(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1147,7 +1147,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseXor$Type$Release(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
@ -1156,7 +1156,7 @@ final class VarHandle$Type$s {
Array handle = (Array)ob;
$type$[] array = ($type$[]) oarray;
return UNSAFE.getAndBitwiseXor$Type$Acquire(array,
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
(((long) Preconditions.checkIndex(index, array.length, Preconditions.AIOOBE_FORMATTER)) << handle.ashift) + handle.abase,
value);
}
#end[Bitwise]

@ -109,12 +109,9 @@ final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
return at.accessModeType(byte[].class, $type$.class, int.class);
}
private static final BiFunction<String, List<Number>, ArrayIndexOutOfBoundsException>
OOBEF = Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new);
@ForceInline
static int index(byte[] ba, int index) {
return Preconditions.checkIndex(index, ba.length - ALIGN, OOBEF);
return Preconditions.checkIndex(index, ba.length - ALIGN, Preconditions.AIOOBE_FORMATTER);
}
@ForceInline

@ -32,7 +32,7 @@ import java.io.OutputStream;
import java.nio.ByteBuffer;
import sun.nio.cs.ISO_8859_1;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
/**
@ -932,8 +932,7 @@ public class Base64 {
public void write(byte[] b, int off, int len) throws IOException {
if (closed)
throw new IOException("Stream is closed");
if (off < 0 || len < 0 || len > b.length - off)
throw new ArrayIndexOutOfBoundsException();
Preconditions.checkFromIndexSize(len, off, b.length, Preconditions.AIOOBE_FORMATTER);
if (len == 0)
return;
if (leftover != 0) {

@ -5178,9 +5178,7 @@ public class Collections {
}
public E get(int index) {
if (index < 0 || index >= n)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+n);
Objects.checkIndex(index, n);
return element;
}

@ -28,7 +28,7 @@ package java.util.zip;
import java.lang.ref.Reference;
import java.nio.ByteBuffer;
import sun.nio.ch.DirectBuffer;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
/**
@ -74,9 +74,7 @@ public class Adler32 implements Checksum {
if (b == null) {
throw new NullPointerException();
}
if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, b.length, Preconditions.AIOOBE_FORMATTER);
adler = updateBytes(adler, b, off, len);
}

@ -30,6 +30,7 @@ import java.nio.ByteBuffer;
import java.util.Objects;
import sun.nio.ch.DirectBuffer;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
/**
@ -73,9 +74,7 @@ public class CRC32 implements Checksum {
if (b == null) {
throw new NullPointerException();
}
if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, b.length, Preconditions.AIOOBE_FORMATTER);
crc = updateBytes(crc, b, off, len);
}
@ -148,15 +147,8 @@ public class CRC32 implements Checksum {
}
Objects.requireNonNull(b);
if (off < 0 || off >= b.length) {
throw new ArrayIndexOutOfBoundsException(off);
}
int endIndex = off + len - 1;
if (endIndex < 0 || endIndex >= b.length) {
throw new ArrayIndexOutOfBoundsException(endIndex);
}
Preconditions.checkIndex(off, b.length, Preconditions.AIOOBE_FORMATTER);
Preconditions.checkIndex(off + len - 1, b.length, Preconditions.AIOOBE_FORMATTER);
}
private static int updateByteBuffer(int alder, long addr,

@ -29,6 +29,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import sun.nio.ch.DirectBuffer;
@ -148,9 +149,7 @@ public final class CRC32C implements Checksum {
if (b == null) {
throw new NullPointerException();
}
if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, b.length, Preconditions.AIOOBE_FORMATTER);
crc = updateBytes(crc, b, off, (off + len));
}

@ -32,6 +32,7 @@ import java.nio.ReadOnlyBufferException;
import java.util.Objects;
import jdk.internal.ref.CleanerFactory;
import jdk.internal.util.Preconditions;
import sun.nio.ch.DirectBuffer;
/**
@ -230,9 +231,7 @@ public class Deflater {
* @see Deflater#needsInput
*/
public void setInput(byte[] input, int off, int len) {
if (off < 0 || len < 0 || off > input.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, input.length, Preconditions.AIOOBE_FORMATTER);
synchronized (zsRef) {
this.input = null;
this.inputArray = input;
@ -297,9 +296,7 @@ public class Deflater {
* @see Inflater#getAdler
*/
public void setDictionary(byte[] dictionary, int off, int len) {
if (off < 0 || len < 0 || off > dictionary.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, dictionary.length, Preconditions.AIOOBE_FORMATTER);
synchronized (zsRef) {
ensureOpen();
setDictionary(zsRef.address(), dictionary, off, len);
@ -556,9 +553,7 @@ public class Deflater {
* @since 1.7
*/
public int deflate(byte[] output, int off, int len, int flush) {
if (off < 0 || len < 0 || off > output.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, output.length, Preconditions.AIOOBE_FORMATTER);
if (flush != NO_FLUSH && flush != SYNC_FLUSH && flush != FULL_FLUSH) {
throw new IllegalArgumentException();
}

@ -32,6 +32,7 @@ import java.nio.ReadOnlyBufferException;
import java.util.Objects;
import jdk.internal.ref.CleanerFactory;
import jdk.internal.util.Preconditions;
import sun.nio.ch.DirectBuffer;
/**
@ -151,9 +152,7 @@ public class Inflater {
* @see Inflater#needsInput
*/
public void setInput(byte[] input, int off, int len) {
if (off < 0 || len < 0 || off > input.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, input.length, Preconditions.AIOOBE_FORMATTER);
synchronized (zsRef) {
this.input = null;
this.inputArray = input;
@ -218,9 +217,7 @@ public class Inflater {
* @see Inflater#getAdler
*/
public void setDictionary(byte[] dictionary, int off, int len) {
if (off < 0 || len < 0 || off > dictionary.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, dictionary.length, Preconditions.AIOOBE_FORMATTER);
synchronized (zsRef) {
ensureOpen();
setDictionary(zsRef.address(), dictionary, off, len);
@ -363,9 +360,7 @@ public class Inflater {
public int inflate(byte[] output, int off, int len)
throws DataFormatException
{
if (off < 0 || len < 0 || off > output.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(len, off, output.length, Preconditions.AIOOBE_FORMATTER);
synchronized (zsRef) {
ensureOpen();
ByteBuffer input = this.input;

@ -36,6 +36,42 @@ import java.util.function.Function;
*/
public class Preconditions {
/**
* Utility exception formatters which can be used in {@code Preconditions}
* check functions below.
*
* These anonymous inner classes can be syntactically replaced by lambda
* expression or method reference, but it's not feasible in practices,
* because {@code Preconditions} is used in many fundamental classes such
* as {@code java.lang.String}, lambda expressions or method references
* exercise many other code at VM startup, this could lead a recursive
* calls when fundamental classes is used in lambda expressions or method
* references.
*/
public static final BiFunction<String, List<Number>, StringIndexOutOfBoundsException>
SIOOBE_FORMATTER = Preconditions.outOfBoundsExceptionFormatter(new Function<>() {
@Override
public StringIndexOutOfBoundsException apply(String s) {
return new StringIndexOutOfBoundsException(s);
}
});
public static final BiFunction<String, List<Number>, ArrayIndexOutOfBoundsException>
AIOOBE_FORMATTER = Preconditions.outOfBoundsExceptionFormatter(new Function<>() {
@Override
public ArrayIndexOutOfBoundsException apply(String s) {
return new ArrayIndexOutOfBoundsException(s);
}
});
public static final BiFunction<String,List<Number>, IndexOutOfBoundsException>
IOOBE_FORMATTER = Preconditions.outOfBoundsExceptionFormatter(new Function<>() {
@Override
public IndexOutOfBoundsException apply(String s) {
return new IndexOutOfBoundsException(s);
}
});
/**
* Maps out-of-bounds values to a runtime exception.
*

@ -35,6 +35,7 @@ import java.io.IOException;
import java.io.FileDescriptor;
import java.util.Set;
import java.util.HashSet;
import java.util.Objects;
import java.util.Collections;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
@ -308,8 +309,7 @@ abstract class AsynchronousSocketChannelImpl
{
if (handler == null)
throw new NullPointerException("'handler' is null");
if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
throw new IndexOutOfBoundsException();
Objects.checkFromIndexSize(offset, length, dsts.length);
ByteBuffer[] bufs = Util.subsequence(dsts, offset, length);
for (int i=0; i<bufs.length; i++) {
if (bufs[i].isReadOnly())
@ -410,8 +410,7 @@ abstract class AsynchronousSocketChannelImpl
{
if (handler == null)
throw new NullPointerException("'handler' is null");
if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
throw new IndexOutOfBoundsException();
Objects.checkFromIndexSize(offset, length, srcs.length);
srcs = Util.subsequence(srcs, offset, length);
write(true, null, srcs, timeout, unit, attachment, handler);
}

@ -35,6 +35,7 @@ import java.util.Objects;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
public class ISO_8859_1
@ -169,24 +170,11 @@ public class ISO_8859_1
byte[] da, int dp, int len) {
Objects.requireNonNull(sa);
Objects.requireNonNull(da);
Preconditions.checkIndex(sp, sa.length, Preconditions.AIOOBE_FORMATTER);
Preconditions.checkIndex(dp, da.length, Preconditions.AIOOBE_FORMATTER);
if (sp < 0 || sp >= sa.length) {
throw new ArrayIndexOutOfBoundsException(sp);
}
if (dp < 0 || dp >= da.length) {
throw new ArrayIndexOutOfBoundsException(dp);
}
int endIndexSP = sp + len - 1;
if (endIndexSP < 0 || endIndexSP >= sa.length) {
throw new ArrayIndexOutOfBoundsException(endIndexSP);
}
int endIndexDP = dp + len - 1;
if (endIndexDP < 0 || endIndexDP >= da.length) {
throw new ArrayIndexOutOfBoundsException(endIndexDP);
}
Preconditions.checkIndex(sp + len - 1, sa.length, Preconditions.AIOOBE_FORMATTER);
Preconditions.checkIndex(dp + len - 1, da.length, Preconditions.AIOOBE_FORMATTER);
}
private CoderResult encodeArrayLoop(CharBuffer src,

@ -31,6 +31,7 @@ import java.security.ProviderException;
import java.util.Arrays;
import java.util.Objects;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
/**
@ -105,9 +106,7 @@ abstract class DigestBase extends MessageDigestSpi implements Cloneable {
if (len == 0) {
return;
}
if ((ofs < 0) || (len < 0) || (ofs > b.length - len)) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(ofs, len, b.length, Preconditions.AIOOBE_FORMATTER);
if (bytesProcessed < 0) {
engineReset();
}
@ -159,10 +158,7 @@ abstract class DigestBase extends MessageDigestSpi implements Cloneable {
}
Objects.requireNonNull(b);
if (ofs < 0 || ofs >= b.length) {
throw new ArrayIndexOutOfBoundsException(ofs);
}
Preconditions.checkIndex(ofs, b.length, Preconditions.AIOOBE_FORMATTER);
int endIndex = (limit / blockSize) * blockSize + blockSize - 1;
if (endIndex >= b.length) {

@ -32,6 +32,8 @@ import java.util.Arrays;
import java.util.Objects;
import static sun.security.provider.ByteArrayAccess.*;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
/**
@ -152,9 +154,7 @@ public final class MD5 extends DigestBase {
// These checks are sufficient for the case when the method
// 'implCompressImpl' is replaced with a compiler
// intrinsic.
if ((ofs < 0) || ((buf.length - ofs) < 64)) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(ofs, 64, buf.length, Preconditions.AIOOBE_FORMATTER);
}
// The method 'implCompress0 seems not to use its parameters.

@ -29,6 +29,8 @@ import java.util.Arrays;
import java.util.Objects;
import static sun.security.provider.ByteArrayAccess.*;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
/**
@ -136,9 +138,7 @@ public final class SHA extends DigestBase {
// Checks similar to those performed by the method 'b2iBig64'
// are sufficient for the case when the method 'implCompress0' is
// replaced with a compiler intrinsic.
if (ofs < 0 || (buf.length - ofs) < 64) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(ofs, 64, buf.length, Preconditions.AIOOBE_FORMATTER);
}
// The method 'implCompress0 seems not to use its parameters.

@ -28,6 +28,7 @@ package sun.security.provider;
import java.util.Arrays;
import java.util.Objects;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import static sun.security.provider.ByteArrayAccess.*;
@ -128,9 +129,7 @@ abstract class SHA2 extends DigestBase {
// Checks similar to those performed by the method 'b2iBig64'
// are sufficient for the case when the method 'implCompress0' is
// replaced with a compiler intrinsic.
if (ofs < 0 || (buf.length - ofs) < 64) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(ofs, 64, buf.length, Preconditions.AIOOBE_FORMATTER);
}
// The method 'implCompressImpl' seems not to use its parameters.

@ -28,6 +28,7 @@ package sun.security.provider;
import java.util.Arrays;
import java.util.Objects;
import jdk.internal.util.Preconditions;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import static sun.security.provider.ByteArrayAccess.*;
@ -229,9 +230,7 @@ abstract class SHA5 extends DigestBase {
// Checks similar to those performed by the method 'b2lBig128'
// are sufficient for the case when the method 'implCompress0' is
// replaced with a compiler intrinsic.
if (ofs < 0 || (buf.length - ofs) < 128) {
throw new ArrayIndexOutOfBoundsException();
}
Preconditions.checkFromIndexSize(ofs, 128, buf.length, Preconditions.AIOOBE_FORMATTER);
}
// The method 'implCompressImpl' seems not to use its parameters.

@ -37,11 +37,6 @@ import jdk.internal.util.Preconditions;
public final class ArrayUtil {
private static final BiFunction<String, List<Number>,
ArrayIndexOutOfBoundsException> AIOOBE_SUPPLIER =
Preconditions.outOfBoundsExceptionFormatter
(ArrayIndexOutOfBoundsException::new);
public static void blockSizeCheck(int len, int blockSize) {
if ((len % blockSize) != 0) {
throw new ProviderException("Internal error in input buffering");
@ -50,7 +45,7 @@ public final class ArrayUtil {
public static void nullAndBoundsCheck(byte[] array, int offset, int len) {
// NPE is thrown when array is null
Preconditions.checkFromIndexSize(offset, len, array.length, AIOOBE_SUPPLIER);
Preconditions.checkFromIndexSize(offset, len, array.length, Preconditions.AIOOBE_FORMATTER);
}
private static void swap(byte[] arr, int i, int j) {

@ -28,6 +28,8 @@ package sun.security.util;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import jdk.internal.util.Preconditions;
/**
* A packed array of booleans.
*
@ -125,9 +127,7 @@ public class BitArray {
* Returns the indexed bit in this BitArray.
*/
public boolean get(int index) throws ArrayIndexOutOfBoundsException {
if (index < 0 || index >= length) {
throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
}
Preconditions.checkIndex(index, length, Preconditions.AIOOBE_FORMATTER);
return (repn[subscript(index)] & position(index)) != 0;
}
@ -137,9 +137,7 @@ public class BitArray {
*/
public void set(int index, boolean value)
throws ArrayIndexOutOfBoundsException {
if (index < 0 || index >= length) {
throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
}
Preconditions.checkIndex(index, length, Preconditions.AIOOBE_FORMATTER);
int idx = subscript(index);
int bit = position(index);

@ -34,6 +34,7 @@ import java.net.SocketOption;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.util.Objects;
/**
@ -139,8 +140,7 @@ class SinkChannelImpl
public long write(ByteBuffer[] srcs, int offset, int length)
throws IOException
{
if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
throw new IndexOutOfBoundsException();
Objects.checkFromIndexSize(offset, length, srcs.length);
try {
return write(Util.subsequence(srcs, offset, length));
} catch (AsynchronousCloseException x) {

@ -33,6 +33,7 @@ import java.io.FileDescriptor;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.util.Objects;
/**
* Pipe.SourceChannel implementation based on socket connection.
@ -120,8 +121,7 @@ class SourceChannelImpl
public long read(ByteBuffer[] dsts, int offset, int length)
throws IOException
{
if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
throw new IndexOutOfBoundsException();
Objects.checkFromIndexSize(offset, length, dsts.length);
try {
return read(Util.subsequence(dsts, offset, length));
} catch (AsynchronousCloseException x) {

@ -39,6 +39,7 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -236,9 +237,7 @@ public class CommandProcessor {
}
String at(int i) {
if (i < 0 || i >= length) {
throw new IndexOutOfBoundsException(String.valueOf(i));
}
Objects.checkIndex(i, length);
return tokens[i];
}
}

@ -753,7 +753,7 @@ public class COFFFileParser {
public int getNumEntries() { return numEntries; }
public DebugDirectoryEntry getEntry(int i) {
if ((i < 0) || (i >= getNumEntries())) throw new IndexOutOfBoundsException();
Objects.checkIndex(i, getNumEntries());
return new DebugDirectoryEntryImpl(offset + i * DEBUG_DIRECTORY_ENTRY_SIZE);
}
}
@ -809,9 +809,7 @@ public class COFFFileParser {
}
public byte getRawDataByte(int i) {
if (i < 0 || i >= getSizeOfData()) {
throw new IndexOutOfBoundsException();
}
Objects.checkIndex(i, getSizeOfData());
seek(getPointerToRawData() + i);
return readByte();
}

@ -79,7 +79,7 @@ public class ConstantPoolCache extends Metadata {
}
public ConstantPoolCacheEntry getEntryAt(int i) {
if (i < 0 || i >= getLength()) throw new IndexOutOfBoundsException(i + " " + getLength());
Objects.checkIndex(i, getLength());
return new ConstantPoolCacheEntry(this, i);
}

@ -28,6 +28,8 @@ package sun.net.httpserver;
import java.nio.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Objects;
import com.sun.net.httpserver.*;
/**
@ -265,9 +267,7 @@ class Request {
assert channel.isBlocking();
if (off < 0 || srclen < 0|| srclen > (b.length-off)) {
throw new IndexOutOfBoundsException ();
}
Objects.checkFromIndexSize(srclen, off, b.length);
if (reset) { /* satisfy from markBuf */
canreturn = markBuf.remaining ();

@ -23,6 +23,7 @@
package jdk.vm.ci.code;
import java.util.Arrays;
import java.util.Objects;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaValue;
@ -233,9 +234,7 @@ public final class BytecodeFrame extends BytecodePosition {
* @throw {@link IndexOutOfBoundsException} if {@code i < 0 || i >= this.numLocals}
*/
public JavaKind getLocalValueKind(int i) {
if (i < 0 || i >= numLocals) {
throw new IndexOutOfBoundsException();
}
Objects.checkIndex(i, numLocals);
return slotKinds[i];
}
@ -247,9 +246,7 @@ public final class BytecodeFrame extends BytecodePosition {
* @throw {@link IndexOutOfBoundsException} if {@code i < 0 || i >= this.numStack}
*/
public JavaKind getStackValueKind(int i) {
if (i < 0 || i >= numStack) {
throw new IndexOutOfBoundsException();
}
Objects.checkIndex(i, numStack);
return slotKinds[i + numLocals];
}
@ -261,9 +258,7 @@ public final class BytecodeFrame extends BytecodePosition {
* @throw {@link IndexOutOfBoundsException} if {@code i < 0 || i >= this.numLocals}
*/
public JavaValue getLocalValue(int i) {
if (i < 0 || i >= numLocals) {
throw new IndexOutOfBoundsException();
}
Objects.checkIndex(i, numLocals);
return values[i];
}
@ -275,9 +270,7 @@ public final class BytecodeFrame extends BytecodePosition {
* @throw {@link IndexOutOfBoundsException} if {@code i < 0 || i >= this.numStack}
*/
public JavaValue getStackValue(int i) {
if (i < 0 || i >= numStack) {
throw new IndexOutOfBoundsException();
}
Objects.checkIndex(i, numStack);
return values[i + numLocals];
}
@ -289,9 +282,7 @@ public final class BytecodeFrame extends BytecodePosition {
* @throw {@link IndexOutOfBoundsException} if {@code i < 0 || i >= this.numLocks}
*/
public JavaValue getLockValue(int i) {
if (i < 0 || i >= numLocks) {
throw new IndexOutOfBoundsException();
}
Objects.checkIndex(i, numLocks);
return values[i + numLocals + numStack];
}

@ -30,7 +30,7 @@
public class Exceptions {
private static boolean ok = true;
private static void fail(Throwable ex, String s, Throwable got) {
private static void fail(Throwable ex, String s, Throwable got) {
ok = false;
System.err.println("expected "
+ ex.getClass().getName() + ": " + ex.getMessage()
@ -49,7 +49,6 @@ public class Exceptions {
try {
thunk.run();
} catch (Throwable x) {
// x.printStackTrace();
if (ex.getClass().isAssignableFrom(x.getClass()))
t = x;
else
@ -94,7 +93,7 @@ public class Exceptions {
System.out.println("StringBuffer.replace(int start, int end, String str)");
tryCatch(" -1, 2, \" \"",
new StringIndexOutOfBoundsException("start -1, end 2, length 7"),
new StringIndexOutOfBoundsException("Range [-1, 2) out of bounds for length 7"),
new Runnable() {
public void run() {
StringBuffer sb = new StringBuffer("hilbert");
@ -102,23 +101,24 @@ public class Exceptions {
}});
tryCatch(" 7, 8, \" \"",
new StringIndexOutOfBoundsException("start 7, end 6, length 6"),
new StringIndexOutOfBoundsException("Range [7, 6) out of bounds for length 6"),
new Runnable() {
public void run() {
StringBuffer sb = new StringBuffer("banach");
sb.replace(7, 8, " ");
}});
tryCatch(" 2, 1, \" \"",
new StringIndexOutOfBoundsException("start 2, end 1, length 7"),
new StringIndexOutOfBoundsException("Range [2, 1) out of bounds for length 7"),
new Runnable() {
public void run() {
StringBuffer sb = new StringBuffer("riemann");
sb.replace(2, 1, " ");
}});
if (!ok)
if (!ok) {
throw new RuntimeException("Some tests FAILED");
else
} else {
System.out.println("All tests PASSED");
}
}
}

@ -49,7 +49,6 @@ public class Exceptions {
try {
thunk.run();
} catch (Throwable x) {
// x.printStackTrace();
if (ex.getClass().isAssignableFrom(x.getClass()))
t = x;
else
@ -94,30 +93,31 @@ public class Exceptions {
System.out.println("StringBuilder.replace(int start, int end, String str)");
tryCatch(" -1, 2, \" \"",
new StringIndexOutOfBoundsException("start -1, end 2, length 7"),
new StringIndexOutOfBoundsException("Range [-1, 2) out of bounds for length 7"),
new Runnable() {
public void run() {
StringBuilder sb = new StringBuilder("hilbert");
sb.replace(-1, 2, " ");
}});
tryCatch(" 7, 8, \" \"",
new StringIndexOutOfBoundsException("start 7, end 6, length 6"),
new StringIndexOutOfBoundsException("Range [7, 6) out of bounds for length 6"),
new Runnable() {
public void run() {
StringBuilder sb = new StringBuilder("banach");
sb.replace(7, 8, " ");
}});
tryCatch(" 2, 1, \" \"",
new StringIndexOutOfBoundsException("start 2, end 1, length 7"),
new StringIndexOutOfBoundsException("Range [2, 1) out of bounds for length 7"),
new Runnable() {
public void run() {
StringBuilder sb = new StringBuilder("riemann");
sb.replace(2, 1, " ");
}});
if (!ok)
if (!ok) {
throw new RuntimeException("Some tests FAILED");
else
} else {
System.out.println("All tests PASSED");
}
}
}
}

@ -34,6 +34,7 @@ package jdk.test.lib.hprof.model;
import jdk.test.lib.hprof.parser.ReadBuffer;
import java.io.IOException;
import java.util.Objects;
/**
* An array of values, that is, an array of ints, boolean, floats or the like.
@ -263,9 +264,7 @@ public class JavaValueArray extends JavaLazyReadObject
}
private void checkIndex(int index) {
if (index < 0 || index >= getLength()) {
throw new ArrayIndexOutOfBoundsException(index);
}
Objects.checkIndex(index, getLength());
}
private void requireType(char type) {

@ -30,6 +30,7 @@ import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
@ -138,9 +139,7 @@ public class CopyLoop {
}
public char charAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
Objects.checkIndex(index, count);
return value[index + offset];
}