From bc22bfa3209c0e0852304b5e33d097d3d6ff92ee Mon Sep 17 00:00:00 2001 From: Nick Hill Date: Tue, 13 Aug 2019 01:52:11 -0700 Subject: [PATCH] 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. --- buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java index 1adb100634..4e185ea787 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java @@ -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() {