From 35215309b97804334610a56e5f5a7660db9c9017 Mon Sep 17 00:00:00 2001 From: Tim Brooks Date: Wed, 13 Jun 2018 12:43:31 -0600 Subject: [PATCH] Make UnpooledHeapByteBuf array methods protected (#8015) Motivation: Currently there is not a clear way to provide a byte array to a netty ByteBuf and be informed when it is released. This is a would be a valuable addition for projects that integrate with netty but also pool their own byte arrays. Modification: Modified the UnpooledHeapByteBuf class so that the freeArray method is protected visibility instead of default. This will allow a user to subclass the UnpooledHeapByteBuf, provide a byte array, and override freeArray to return the byte array to a pool when it is called. Additionally this makes this implementation equivalent to UnpooledDirectByteBuf (freeDirect is protected). Additionally allocateArray is also made protect to provide another override option for subclasses. Result: Users can override UnpooledHeapByteBuf#freeArray and UnpooledHeapByteBuf#allocateArray. --- .../java/io/netty/buffer/UnpooledByteBufAllocator.java | 8 ++++---- .../main/java/io/netty/buffer/UnpooledHeapByteBuf.java | 4 ++-- .../java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java b/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java index 4edf0dcd3d..6fe188ed8b 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java @@ -140,14 +140,14 @@ public final class UnpooledByteBufAllocator extends AbstractByteBufAllocator imp } @Override - byte[] allocateArray(int initialCapacity) { + protected byte[] allocateArray(int initialCapacity) { byte[] bytes = super.allocateArray(initialCapacity); ((UnpooledByteBufAllocator) alloc()).incrementHeap(bytes.length); return bytes; } @Override - void freeArray(byte[] array) { + protected void freeArray(byte[] array) { int length = array.length; super.freeArray(array); ((UnpooledByteBufAllocator) alloc()).decrementHeap(length); @@ -160,14 +160,14 @@ public final class UnpooledByteBufAllocator extends AbstractByteBufAllocator imp } @Override - byte[] allocateArray(int initialCapacity) { + protected byte[] allocateArray(int initialCapacity) { byte[] bytes = super.allocateArray(initialCapacity); ((UnpooledByteBufAllocator) alloc()).incrementHeap(bytes.length); return bytes; } @Override - void freeArray(byte[] array) { + protected void freeArray(byte[] array) { int length = array.length; super.freeArray(array); ((UnpooledByteBufAllocator) alloc()).decrementHeap(length); diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java index 26b1c5122b..0133845f82 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java @@ -84,11 +84,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { setIndex(0, initialArray.length); } - byte[] allocateArray(int initialCapacity) { + protected byte[] allocateArray(int initialCapacity) { return new byte[initialCapacity]; } - void freeArray(byte[] array) { + protected void freeArray(byte[] array) { // NOOP } diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java index 51786f1567..0fbe856466 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeHeapByteBuf.java @@ -30,7 +30,7 @@ class UnpooledUnsafeHeapByteBuf extends UnpooledHeapByteBuf { } @Override - byte[] allocateArray(int initialCapacity) { + protected byte[] allocateArray(int initialCapacity) { return PlatformDependent.allocateUninitializedArray(initialCapacity); }