diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java index 6661e65bc9..897c8f33ff 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java @@ -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(); diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java index 5373bdac21..f859217c2c 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java @@ -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);