Fix redundant or missing checks and other inconsistencies in ByteBuf impls (#9119)
Motivation There are a few minor inconsistencies / redundant operations in the ByteBuf implementations which would be good to fix. Modifications - Unnecessary ByteBuffer.duplicate() performed in CompositeByteBuf.nioBuffer(int,int) - Add missing checkIndex(...) check to ReadOnlyByteBufferBuf.nioBuffer(int,int) - Remove duplicate bounds check in ReadOnlyByteBufferBuf.getBytes(int,byte[],int,int) - Omit redundant bounds check in UnpooledHeapByteBuf.getBytes(int,ByteBuffer) Result More consistency and slightly less overhead
This commit is contained in:
parent
e17ce934da
commit
385dadcfbc
@ -1389,30 +1389,31 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
|||||||
checkIndex0(index, fieldLength);
|
checkIndex0(index, fieldLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkRangeBounds(final int index, final int fieldLength, final int capacity) {
|
private static void checkRangeBounds(final String indexName, final int index,
|
||||||
|
final int fieldLength, final int capacity) {
|
||||||
if (isOutOfBounds(index, fieldLength, capacity)) {
|
if (isOutOfBounds(index, fieldLength, capacity)) {
|
||||||
throw new IndexOutOfBoundsException(String.format(
|
throw new IndexOutOfBoundsException(String.format(
|
||||||
"index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity));
|
"%s: %d, length: %d (expected: range(0, %d))", indexName, index, fieldLength, capacity));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final void checkIndex0(int index, int fieldLength) {
|
final void checkIndex0(int index, int fieldLength) {
|
||||||
if (checkBounds) {
|
if (checkBounds) {
|
||||||
checkRangeBounds(index, fieldLength, capacity());
|
checkRangeBounds("index", index, fieldLength, capacity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final void checkSrcIndex(int index, int length, int srcIndex, int srcCapacity) {
|
protected final void checkSrcIndex(int index, int length, int srcIndex, int srcCapacity) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
if (checkBounds) {
|
if (checkBounds) {
|
||||||
checkRangeBounds(srcIndex, length, srcCapacity);
|
checkRangeBounds("srcIndex", srcIndex, length, srcCapacity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final void checkDstIndex(int index, int length, int dstIndex, int dstCapacity) {
|
protected final void checkDstIndex(int index, int length, int dstIndex, int dstCapacity) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
if (checkBounds) {
|
if (checkBounds) {
|
||||||
checkRangeBounds(dstIndex, length, dstCapacity);
|
checkRangeBounds("dstIndex", dstIndex, length, dstCapacity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1630,7 +1630,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
|
|||||||
ByteBuffer[] buffers = nioBuffers(index, length);
|
ByteBuffer[] buffers = nioBuffers(index, length);
|
||||||
|
|
||||||
if (buffers.length == 1) {
|
if (buffers.length == 1) {
|
||||||
return buffers[0].duplicate();
|
return buffers[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer merged = ByteBuffer.allocate(length).order(order());
|
ByteBuffer merged = ByteBuffer.allocate(length).order(order());
|
||||||
|
@ -195,11 +195,6 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
|
|||||||
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
||||||
checkDstIndex(index, length, dstIndex, dst.length);
|
checkDstIndex(index, length, dstIndex, dst.length);
|
||||||
|
|
||||||
if (dstIndex < 0 || dstIndex > dst.length - length) {
|
|
||||||
throw new IndexOutOfBoundsException(String.format(
|
|
||||||
"dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteBuffer tmpBuf = internalNioBuffer();
|
ByteBuffer tmpBuf = internalNioBuffer();
|
||||||
tmpBuf.clear().position(index).limit(index + length);
|
tmpBuf.clear().position(index).limit(index + length);
|
||||||
tmpBuf.get(dst, dstIndex, length);
|
tmpBuf.get(dst, dstIndex, length);
|
||||||
@ -449,6 +444,7 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuffer nioBuffer(int index, int length) {
|
public ByteBuffer nioBuffer(int index, int length) {
|
||||||
|
checkIndex(index, length);
|
||||||
return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
|
return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +194,7 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
||||||
checkIndex(index, dst.remaining());
|
ensureAccessible();
|
||||||
dst.put(array, index, dst.remaining());
|
dst.put(array, index, dst.remaining());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user