From d353d5936b582681ca215d0029960e22a6d8764c Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 30 Oct 2015 15:37:16 -0700 Subject: [PATCH] Move isValid utility method to MathUtil Motivation: In 4.1 and master the isValid utility has been moved to MathUtil. We should stay consistent for internal APIs. Modifications: - Move isValid to MathUtil Result: More consistent internal structure across branches. --- .../main/java/io/netty/buffer/AbstractByteBuf.java | 11 ++++------- .../main/java/io/netty/buffer/UnsafeByteBufUtil.java | 7 ++++--- .../main/java/io/netty/util/internal/MathUtil.java | 12 ++++++++++++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index 0adb04a052..34ccab7a85 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -32,6 +32,7 @@ import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; +import static io.netty.util.internal.MathUtil.isOutOfBounds; /** * A skeletal implementation of a buffer. @@ -1137,7 +1138,7 @@ public abstract class AbstractByteBuf extends ByteBuf { } final void checkIndex0(int index, int fieldLength) { - if (isInvalid(index, fieldLength, capacity())) { + if (isOutOfBounds(index, fieldLength, capacity())) { throw new IndexOutOfBoundsException(String.format( "index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity())); } @@ -1145,7 +1146,7 @@ public abstract class AbstractByteBuf extends ByteBuf { protected final void checkSrcIndex(int index, int length, int srcIndex, int srcCapacity) { checkIndex(index, length); - if (isInvalid(srcIndex, length, srcCapacity)) { + if (isOutOfBounds(srcIndex, length, srcCapacity)) { throw new IndexOutOfBoundsException(String.format( "srcIndex: %d, length: %d (expected: range(0, %d))", srcIndex, length, srcCapacity)); } @@ -1153,16 +1154,12 @@ public abstract class AbstractByteBuf extends ByteBuf { protected final void checkDstIndex(int index, int length, int dstIndex, int dstCapacity) { checkIndex(index, length); - if (isInvalid(dstIndex, length, dstCapacity)) { + if (isOutOfBounds(dstIndex, length, dstCapacity)) { throw new IndexOutOfBoundsException(String.format( "dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dstCapacity)); } } - static boolean isInvalid(int index, int length, int capacity) { - return (index | length | (index + length) | (capacity - (index + length))) < 0; - } - /** * Throws an {@link IndexOutOfBoundsException} if the current * {@linkplain #readableBytes() readable bytes} of this buffer is less diff --git a/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java index 9fa3fbf7bf..24a5ca9e7f 100644 --- a/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java @@ -23,6 +23,7 @@ import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import static io.netty.util.internal.MathUtil.isOutOfBounds; import static io.netty.util.internal.ObjectUtil.checkNotNull; /** @@ -282,7 +283,7 @@ final class UnsafeByteBufUtil { static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuf dst, int dstIndex, int length) { buf.checkIndex(index, length); checkNotNull(dst, "dst"); - if (AbstractByteBuf.isInvalid(dstIndex, length, dst.capacity())) { + if (isOutOfBounds(dstIndex, length, dst.capacity())) { throw new IndexOutOfBoundsException("dstIndex: " + dstIndex); } @@ -298,7 +299,7 @@ final class UnsafeByteBufUtil { static void getBytes(AbstractByteBuf buf, long addr, int index, byte[] dst, int dstIndex, int length) { buf.checkIndex(index, length); checkNotNull(dst, "dst"); - if (AbstractByteBuf.isInvalid(dstIndex, length, dst.length)) { + if (isOutOfBounds(dstIndex, length, dst.length)) { throw new IndexOutOfBoundsException("dstIndex: " + dstIndex); } if (length != 0) { @@ -328,7 +329,7 @@ final class UnsafeByteBufUtil { static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuf src, int srcIndex, int length) { buf.checkIndex(index, length); checkNotNull(src, "src"); - if (AbstractByteBuf.isInvalid(srcIndex, length, src.capacity())) { + if (isOutOfBounds(srcIndex, length, src.capacity())) { throw new IndexOutOfBoundsException("srcIndex: " + srcIndex); } diff --git a/common/src/main/java/io/netty/util/internal/MathUtil.java b/common/src/main/java/io/netty/util/internal/MathUtil.java index 7b22bcf931..d4bcb1bcaa 100644 --- a/common/src/main/java/io/netty/util/internal/MathUtil.java +++ b/common/src/main/java/io/netty/util/internal/MathUtil.java @@ -36,4 +36,16 @@ public final class MathUtil { assert value > Integer.MIN_VALUE && value < 0x40000000; return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); } + + /** + * Determine if the requested {@code index} and {@code length} will fit within {@code capacity}. + * @param index The starting index. + * @param length The length which will be utilized (starting from {@code index}). + * @param capacity The capacity that {@code index + length} is allowed to be within. + * @return {@code true} if the requested {@code index} and {@code length} will fit within {@code capacity}. + * {@code false} if this would result in an index out of bounds exception. + */ + public static boolean isOutOfBounds(int index, int length, int capacity) { + return (index | length | (index + length) | (capacity - (index + length))) < 0; + } }