Allow to override how wrapped direct ByteBuffer are allocated to make it easier to extend
This commit is contained in:
parent
329bbfcd87
commit
9b7d286652
@ -102,6 +102,13 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
||||
leak = leakDetector.open(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a new direct {@link ByteBuffer} with the given initialCapacity.
|
||||
*/
|
||||
protected ByteBuffer allocateDirect(int initialCapacity) {
|
||||
return ByteBuffer.allocateDirect(initialCapacity);
|
||||
}
|
||||
|
||||
private void setByteBuffer(ByteBuffer buffer) {
|
||||
ByteBuffer oldBuffer = this.buffer;
|
||||
if (oldBuffer != null) {
|
||||
@ -140,7 +147,7 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
||||
int oldCapacity = capacity;
|
||||
if (newCapacity > oldCapacity) {
|
||||
ByteBuffer oldBuffer = buffer;
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity);
|
||||
ByteBuffer newBuffer = allocateDirect(newCapacity);
|
||||
oldBuffer.position(0).limit(oldBuffer.capacity());
|
||||
newBuffer.position(0).limit(oldBuffer.capacity());
|
||||
newBuffer.put(oldBuffer);
|
||||
@ -148,7 +155,7 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
||||
setByteBuffer(newBuffer);
|
||||
} else if (newCapacity < oldCapacity) {
|
||||
ByteBuffer oldBuffer = buffer;
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity);
|
||||
ByteBuffer newBuffer = allocateDirect(newCapacity);
|
||||
if (readerIndex < newCapacity) {
|
||||
if (writerIndex > newCapacity) {
|
||||
writerIndex(writerIndex = newCapacity);
|
||||
@ -553,7 +560,7 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
||||
}
|
||||
|
||||
ByteBuffer dst =
|
||||
src.isDirect()? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length);
|
||||
src.isDirect()? allocateDirect(length) : ByteBuffer.allocate(length);
|
||||
dst.put(src);
|
||||
dst.order(order());
|
||||
dst.clear();
|
||||
|
@ -68,7 +68,7 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
|
||||
}
|
||||
|
||||
this.alloc = alloc;
|
||||
setByteBuffer(ByteBuffer.allocateDirect(initialCapacity));
|
||||
setByteBuffer(allocateDirect(initialCapacity));
|
||||
leak = leakDetector.open(this);
|
||||
}
|
||||
|
||||
@ -105,6 +105,13 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
|
||||
leak = leakDetector.open(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a new direct {@link ByteBuffer} with the given initialCapacity.
|
||||
*/
|
||||
protected ByteBuffer allocateDirect(int initialCapacity) {
|
||||
return ByteBuffer.allocateDirect(initialCapacity);
|
||||
}
|
||||
|
||||
private void setByteBuffer(ByteBuffer buffer) {
|
||||
ByteBuffer oldBuffer = this.buffer;
|
||||
if (oldBuffer != null) {
|
||||
@ -144,7 +151,7 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
|
||||
int oldCapacity = capacity;
|
||||
if (newCapacity > oldCapacity) {
|
||||
ByteBuffer oldBuffer = buffer;
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity);
|
||||
ByteBuffer newBuffer = allocateDirect(newCapacity);
|
||||
oldBuffer.position(0).limit(oldBuffer.capacity());
|
||||
newBuffer.position(0).limit(oldBuffer.capacity());
|
||||
newBuffer.put(oldBuffer);
|
||||
@ -152,7 +159,7 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
|
||||
setByteBuffer(newBuffer);
|
||||
} else if (newCapacity < oldCapacity) {
|
||||
ByteBuffer oldBuffer = buffer;
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity);
|
||||
ByteBuffer newBuffer = allocateDirect(newCapacity);
|
||||
if (readerIndex < newCapacity) {
|
||||
if (writerIndex > newCapacity) {
|
||||
writerIndex(writerIndex = newCapacity);
|
||||
|
Loading…
Reference in New Issue
Block a user