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.
This commit is contained in:
Tim Brooks 2018-06-13 12:43:31 -06:00 committed by Norman Maurer
parent 400ca87334
commit 35215309b9
3 changed files with 7 additions and 7 deletions

View File

@ -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);

View File

@ -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
}

View File

@ -30,7 +30,7 @@ class UnpooledUnsafeHeapByteBuf extends UnpooledHeapByteBuf {
}
@Override
byte[] allocateArray(int initialCapacity) {
protected byte[] allocateArray(int initialCapacity) {
return PlatformDependent.allocateUninitializedArray(initialCapacity);
}