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
f966a0a041
commit
1e575d7389
@ -1335,30 +1335,31 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
||||
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)) {
|
||||
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) {
|
||||
if (checkBounds) {
|
||||
checkRangeBounds(index, fieldLength, capacity());
|
||||
checkRangeBounds("index", index, fieldLength, capacity());
|
||||
}
|
||||
}
|
||||
|
||||
protected final void checkSrcIndex(int index, int length, int srcIndex, int srcCapacity) {
|
||||
checkIndex(index, length);
|
||||
if (checkBounds) {
|
||||
checkRangeBounds(srcIndex, length, srcCapacity);
|
||||
checkRangeBounds("srcIndex", srcIndex, length, srcCapacity);
|
||||
}
|
||||
}
|
||||
|
||||
protected final void checkDstIndex(int index, int length, int dstIndex, int dstCapacity) {
|
||||
checkIndex(index, length);
|
||||
if (checkBounds) {
|
||||
checkRangeBounds(dstIndex, length, dstCapacity);
|
||||
checkRangeBounds("dstIndex", dstIndex, length, dstCapacity);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1629,7 +1629,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
|
||||
ByteBuffer[] buffers = nioBuffers(index, length);
|
||||
|
||||
if (buffers.length == 1) {
|
||||
return buffers[0].duplicate();
|
||||
return buffers[0];
|
||||
}
|
||||
|
||||
ByteBuffer merged = ByteBuffer.allocate(length).order(order());
|
||||
|
@ -194,11 +194,6 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
|
||||
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int 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();
|
||||
tmpBuf.clear().position(index).limit(index + length);
|
||||
tmpBuf.get(dst, dstIndex, length);
|
||||
@ -448,6 +443,7 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
|
||||
|
||||
@Override
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
|
||||
}
|
||||
|
||||
|
@ -194,7 +194,7 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
||||
checkIndex(index, dst.remaining());
|
||||
ensureAccessible();
|
||||
dst.put(array, index, dst.remaining());
|
||||
return this;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user