8276806: Use Objects.checkFromIndexSize where possible in java.base

Reviewed-by: rriggs, lancea
This commit is contained in:
Sergey Tsypanov 2021-12-02 20:00:49 +00:00 committed by Roger Riggs
parent 30087cc1b8
commit 73a9654c26
11 changed files with 40 additions and 43 deletions

View file

@ -1035,10 +1035,7 @@ public class ObjectInputStream
if (buf == null) {
throw new NullPointerException();
}
int endoff = off + len;
if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromIndexSize(off, len, buf.length);
return bin.read(buf, off, len, false);
}
@ -1207,10 +1204,7 @@ public class ObjectInputStream
* @throws IOException If other I/O error has occurred.
*/
public void readFully(byte[] buf, int off, int len) throws IOException {
int endoff = off + len;
if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromToIndex(off, len, buf.length);
bin.readFully(buf, off, len, false);
}

View file

@ -32,6 +32,7 @@ import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -718,10 +719,7 @@ public class ObjectOutputStream
if (buf == null) {
throw new NullPointerException();
}
int endoff = off + len;
if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromIndexSize(off, len, buf.length);
bout.write(buf, off, len, false);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,8 @@
package java.io;
import java.util.Objects;
/**
* A piped input stream should be connected
* to a piped output stream; the piped input
@ -367,9 +369,9 @@ public class PipedInputStream extends InputStream {
public synchronized int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,8 @@
package java.io;
import java.util.Objects;
/**
* A {@code PushbackInputStream} adds
* functionality to another input stream, namely
@ -162,9 +164,9 @@ public class PushbackInputStream extends FilterInputStream {
ensureOpen();
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}

View file

@ -25,8 +25,8 @@
package java.io;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Objects;
import java.util.Vector;
/**
@ -189,9 +189,9 @@ public class SequenceInputStream extends InputStream {
return -1;
} else if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}
do {

View file

@ -430,9 +430,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
* {@code beginIndex} is larger than {@code endIndex}.
*/
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
Preconditions.checkFromToIndex(beginIndex, endIndex, length(), null);
if (isLatin1()) {
return endIndex - beginIndex;
}

View file

@ -28,6 +28,7 @@ package java.util.zip;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Objects;
/**
* Implements an input stream filter for compressing data in the "deflate"
@ -172,9 +173,9 @@ public class DeflaterInputStream extends FilterInputStream {
ensureOpen();
if (b == null) {
throw new NullPointerException("Null buffer for read");
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}

View file

@ -29,6 +29,7 @@ import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.EOFException;
import java.util.Objects;
/**
* This class implements a stream filter for uncompressing data in the
@ -142,9 +143,9 @@ public class InflaterInputStream extends FilterInputStream {
ensureOpen();
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}
try {

View file

@ -28,6 +28,7 @@ package java.util.zip;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;
/**
* Implements an output stream filter for uncompressing data stored in the
@ -223,9 +224,9 @@ public class InflaterOutputStream extends FilterOutputStream {
ensureOpen();
if (b == null) {
throw new NullPointerException("Null buffer for read");
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,6 +30,7 @@ import java.io.IOException;
import java.io.EOFException;
import java.io.PushbackInputStream;
import java.nio.charset.Charset;
import java.util.Objects;
import sun.nio.cs.UTF_8;
@ -182,9 +183,8 @@ public class ZipInputStream extends InflaterInputStream implements ZipConstants
*/
public int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if (off < 0 || len < 0 || off > b.length - len) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}

View file

@ -28,6 +28,7 @@ package java.util.zip;
import java.io.OutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.Vector;
import java.util.HashSet;
import static java.util.zip.ZipConstants64.*;
@ -333,9 +334,8 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant
throws IOException
{
ensureOpen();
if (off < 0 || len < 0 || off > b.length - len) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return;
}