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:
parent
400ca87334
commit
35215309b9
@ -140,14 +140,14 @@ public final class UnpooledByteBufAllocator extends AbstractByteBufAllocator imp
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
byte[] allocateArray(int initialCapacity) {
|
protected byte[] allocateArray(int initialCapacity) {
|
||||||
byte[] bytes = super.allocateArray(initialCapacity);
|
byte[] bytes = super.allocateArray(initialCapacity);
|
||||||
((UnpooledByteBufAllocator) alloc()).incrementHeap(bytes.length);
|
((UnpooledByteBufAllocator) alloc()).incrementHeap(bytes.length);
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void freeArray(byte[] array) {
|
protected void freeArray(byte[] array) {
|
||||||
int length = array.length;
|
int length = array.length;
|
||||||
super.freeArray(array);
|
super.freeArray(array);
|
||||||
((UnpooledByteBufAllocator) alloc()).decrementHeap(length);
|
((UnpooledByteBufAllocator) alloc()).decrementHeap(length);
|
||||||
@ -160,14 +160,14 @@ public final class UnpooledByteBufAllocator extends AbstractByteBufAllocator imp
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
byte[] allocateArray(int initialCapacity) {
|
protected byte[] allocateArray(int initialCapacity) {
|
||||||
byte[] bytes = super.allocateArray(initialCapacity);
|
byte[] bytes = super.allocateArray(initialCapacity);
|
||||||
((UnpooledByteBufAllocator) alloc()).incrementHeap(bytes.length);
|
((UnpooledByteBufAllocator) alloc()).incrementHeap(bytes.length);
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void freeArray(byte[] array) {
|
protected void freeArray(byte[] array) {
|
||||||
int length = array.length;
|
int length = array.length;
|
||||||
super.freeArray(array);
|
super.freeArray(array);
|
||||||
((UnpooledByteBufAllocator) alloc()).decrementHeap(length);
|
((UnpooledByteBufAllocator) alloc()).decrementHeap(length);
|
||||||
|
@ -84,11 +84,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
|
|||||||
setIndex(0, initialArray.length);
|
setIndex(0, initialArray.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] allocateArray(int initialCapacity) {
|
protected byte[] allocateArray(int initialCapacity) {
|
||||||
return new byte[initialCapacity];
|
return new byte[initialCapacity];
|
||||||
}
|
}
|
||||||
|
|
||||||
void freeArray(byte[] array) {
|
protected void freeArray(byte[] array) {
|
||||||
// NOOP
|
// NOOP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ class UnpooledUnsafeHeapByteBuf extends UnpooledHeapByteBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
byte[] allocateArray(int initialCapacity) {
|
protected byte[] allocateArray(int initialCapacity) {
|
||||||
return PlatformDependent.allocateUninitializedArray(initialCapacity);
|
return PlatformDependent.allocateUninitializedArray(initialCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user