Utilize i.n.u.internal.ObjectUtil to assert Preconditions (buffer) (#11170) (#11182)

Motivation:

NullChecks resulting in a NullPointerException or IllegalArgumentException, numeric ranges (>0, >=0) checks, not empty strings/arrays checks must never be anonymous but with the parameter or variable name which is checked. They must be specific and should not be done with an "OR-Logic" (if a == null || b == null) throw new NullPointerEx.

Modifications:

* import static relevant checks
* Replace manual checks with ObjectUtil methods

Result:

All checks needed are done with ObjectUtil, some exception texts are improved.

Fixes #11170
This commit is contained in:
Boris Unckel 2021-04-22 14:20:44 +02:00 committed by Norman Maurer
parent eb563c25b4
commit 73bdca9442
2 changed files with 9 additions and 4 deletions

View File

@ -16,6 +16,7 @@
package io.netty.buffer;
import static java.util.Objects.requireNonNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.StringUtil;
@ -111,7 +112,7 @@ public class ByteBufInputStream extends InputStream implements DataInput {
if (releaseOnClose) {
buffer.release();
}
throw new IllegalArgumentException("length: " + length);
checkPositiveOrZero(length, "length");
}
if (length > buffer.readableBytes()) {
if (releaseOnClose) {

View File

@ -249,9 +249,13 @@ public final class ByteBufUtil {
* {@code a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length]}
*/
public static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length) {
if (aStartIndex < 0 || bStartIndex < 0 || length < 0) {
throw new IllegalArgumentException("All indexes and lengths must be non-negative");
}
requireNonNull(a, "a");
requireNonNull(b, "b");
// All indexes and lengths must be non-negative
checkPositiveOrZero(aStartIndex, "aStartIndex");
checkPositiveOrZero(bStartIndex, "bStartIndex");
checkPositiveOrZero(length, "length");
if (a.writerIndex() - length < aStartIndex || b.writerIndex() - length < bStartIndex) {
return false;
}