Use alloc().heapBuffer(...) to allocate new heap buffer.

Motivation

Underlying array allocations in UnpooledHeapByteBuf are intended be done
via the protected allocateArray(int) method, so that they can be tracked
and/or overridden by subclasses, for example
UnpooledByteBufAllocator$InstrumentedUnpooledHeapByteBuf or #8015. But
it looks like an explicit allocation was missed in the copy(int,int)
method.

Modification

Just use alloc().heapBuffer(...) for the allocation

Result

No possibility of "missing" array allocations when ByteBuf#copy is used.
This commit is contained in:
Nick Hill 2019-08-13 01:52:11 -07:00 committed by Norman Maurer
parent 6862ab76c0
commit 695aa0959d

View File

@ -536,9 +536,7 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
@Override
public ByteBuf copy(int index, int length) {
checkIndex(index, length);
byte[] copiedArray = PlatformDependent.allocateUninitializedArray(length);
System.arraycopy(array, index, copiedArray, 0, length);
return new UnpooledHeapByteBuf(alloc(), copiedArray, maxCapacity());
return alloc().heapBuffer(length, maxCapacity()).writeBytes(array, index, length);
}
private ByteBuffer internalNioBuffer() {