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

View file

@ -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) {

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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,

View file

@ -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));
}

View file

@ -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();
}

View file

@ -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;