Class PooledByteBuf<T>
- All Implemented Interfaces:
ByteBufConvertible
,ReferenceCounted
,Comparable<ByteBuf>
- Direct Known Subclasses:
PooledDirectByteBuf
,PooledHeapByteBuf
,PooledUnsafeDirectByteBuf
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ByteBufAllocator
(package private) PoolThreadCache
protected long
protected int
(package private) int
protected T
protected int
private final Recycler.EnhancedHandle
<PooledByteBuf<T>> (package private) ByteBuffer
Fields inherited from class io.netty.buffer.AbstractByteBuf
checkAccessible, leakDetector, readerIndex, writerIndex
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
PooledByteBuf
(ObjectPool.Handle<? extends PooledByteBuf<T>> recyclerHandle, int maxCapacity) -
Method Summary
Modifier and TypeMethodDescription(package private) final ByteBuffer
_internalNioBuffer
(int index, int length, boolean duplicate) final ByteBufAllocator
alloc()
Returns theByteBufAllocator
which created this buffer.final int
capacity()
Returns the number of bytes (octets) this buffer can contain.final ByteBuf
capacity
(int newCapacity) Adjusts the capacity of this buffer.protected final void
Called onceAbstractReferenceCountedByteBuf.refCnt()
is equals 0.(package private) ByteBuffer
duplicateInternalNioBuffer
(int index, int length) final int
getBytes
(int index, FileChannel out, long position, int length) Transfers this buffer's data starting at the specified absoluteindex
to the specified channel starting at the given file position.final int
getBytes
(int index, GatheringByteChannel out, int length) Transfers this buffer's data to the specified channel starting at the specified absoluteindex
.protected final int
idx
(int index) (package private) void
init
(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength, PoolThreadCache cache) private void
init0
(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength, PoolThreadCache cache) (package private) void
initUnpooled
(PoolChunk<T> chunk, int length) protected final ByteBuffer
final ByteBuffer
internalNioBuffer
(int index, int length) Internal use only: Exposes the internal NIO buffer.final boolean
Returnstrue
if thisByteBuf
implementation is backed by a single memory region.int
Returns the maximum number of bytes which can be written for certain without involving an internal reallocation or data-copy.protected abstract ByteBuffer
newInternalNioBuffer
(T memory) final ByteBuffer
nioBuffer
(int index, int length) Exposes this buffer's sub-region as an NIOByteBuffer
.final int
Returns the maximum number of NIOByteBuffer
s that consist this buffer.final ByteBuffer[]
nioBuffers
(int index, int length) Exposes this buffer's bytes as an NIOByteBuffer
's for the specified index and length The returned buffer either share or contains the copied content of this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer.final ByteOrder
order()
Returns the endianness of this buffer.final int
readBytes
(FileChannel out, long position, int length) Transfers this buffer's data starting at the currentreaderIndex
to the specified channel starting at the given file position.final int
readBytes
(GatheringByteChannel out, int length) Transfers this buffer's data to the specified stream starting at the currentreaderIndex
.final ByteBuf
Returns a retained buffer which shares the whole region of this buffer.final ByteBuf
Returns a retained slice of this buffer's readable bytes.final ByteBuf
retainedSlice
(int index, int length) Returns a retained slice of this buffer's sub-region.(package private) final void
reuse
(int maxCapacity) Method must be called before reuse thisPooledByteBufAllocator
final int
setBytes
(int index, FileChannel in, long position, int length) Transfers the content of the specified source channel starting at the given file position to this buffer starting at the specified absoluteindex
.final int
setBytes
(int index, ScatteringByteChannel in, int length) Transfers the content of the specified source channel to this buffer starting at the specified absoluteindex
.final ByteBuf
unwrap()
Return the underlying buffer instance if this buffer is a wrapper of another buffer.Methods inherited from class io.netty.buffer.AbstractReferenceCountedByteBuf
isAccessible, refCnt, release, release, resetRefCnt, retain, retain, setRefCnt, touch, touch
Methods inherited from class io.netty.buffer.AbstractByteBuf
_getByte, _getInt, _getIntLE, _getLong, _getLongLE, _getShort, _getShortLE, _getUnsignedMedium, _getUnsignedMediumLE, _setByte, _setInt, _setIntLE, _setLong, _setLongLE, _setMedium, _setMediumLE, _setShort, _setShortLE, adjustMarkers, asReadOnly, bytesBefore, bytesBefore, bytesBefore, checkDstIndex, checkDstIndex, checkIndex, checkIndex, checkIndex0, checkNewCapacity, checkReadableBytes, checkSrcIndex, clear, compareTo, copy, discardMarks, discardReadBytes, discardSomeReadBytes, duplicate, ensureAccessible, ensureWritable, ensureWritable, ensureWritable0, equals, forEachByte, forEachByte, forEachByteAsc0, forEachByteDesc, forEachByteDesc, forEachByteDesc0, getBoolean, getByte, getBytes, getBytes, getBytes, getChar, getCharSequence, getDouble, getFloat, getInt, getIntLE, getLong, getLongLE, getMedium, getMediumLE, getShort, getShortLE, getUnsignedByte, getUnsignedInt, getUnsignedIntLE, getUnsignedMedium, getUnsignedMediumLE, getUnsignedShort, getUnsignedShortLE, hashCode, indexOf, isReadable, isReadable, isReadOnly, isWritable, isWritable, markReaderIndex, markWriterIndex, maxCapacity, maxCapacity, maxWritableBytes, newSwappedByteBuf, nioBuffer, nioBuffers, order, readableBytes, readBoolean, readByte, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readChar, readCharSequence, readDouble, readerIndex, readerIndex, readFloat, readInt, readIntLE, readLong, readLongLE, readMedium, readMediumLE, readRetainedSlice, readShort, readShortLE, readSlice, readUnsignedByte, readUnsignedInt, readUnsignedIntLE, readUnsignedMedium, readUnsignedMediumLE, readUnsignedShort, readUnsignedShortLE, resetReaderIndex, resetWriterIndex, setBoolean, setByte, setBytes, setBytes, setBytes, setChar, setCharSequence, setDouble, setFloat, setIndex, setIndex0, setInt, setIntLE, setLong, setLongLE, setMedium, setMediumLE, setShort, setShortLE, setZero, skipBytes, slice, slice, toString, toString, toString, trimIndicesToCapacity, writableBytes, writeBoolean, writeByte, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeChar, writeCharSequence, writeDouble, writeFloat, writeInt, writeIntLE, writeLong, writeLongLE, writeMedium, writeMediumLE, writerIndex, writerIndex, writeShort, writeShortLE, writeZero
Methods inherited from class io.netty.buffer.ByteBuf
array, arrayOffset, asByteBuf, copy, getBytes, getBytes, getBytes, getBytes, getDoubleLE, getFloatLE, hasArray, hasMemoryAddress, isDirect, memoryAddress, readDoubleLE, readFloatLE, setBytes, setBytes, setBytes, setBytes, setDoubleLE, setFloatLE, writeDoubleLE, writeFloatLE
-
Field Details
-
recyclerHandle
-
chunk
-
handle
protected long handle -
memory
-
offset
protected int offset -
length
protected int length -
maxLength
int maxLength -
cache
PoolThreadCache cache -
tmpNioBuf
ByteBuffer tmpNioBuf -
allocator
-
-
Constructor Details
-
PooledByteBuf
protected PooledByteBuf(ObjectPool.Handle<? extends PooledByteBuf<T>> recyclerHandle, int maxCapacity)
-
-
Method Details
-
init
void init(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength, PoolThreadCache cache) -
initUnpooled
-
init0
private void init0(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength, PoolThreadCache cache) -
reuse
final void reuse(int maxCapacity) Method must be called before reuse thisPooledByteBufAllocator
-
capacity
public final int capacity()Description copied from class:ByteBuf
Returns the number of bytes (octets) this buffer can contain. -
maxFastWritableBytes
public int maxFastWritableBytes()Description copied from class:ByteBuf
Returns the maximum number of bytes which can be written for certain without involving an internal reallocation or data-copy. The returned value will be ≥ByteBuf.writableBytes()
and ≤ByteBuf.maxWritableBytes()
.- Overrides:
maxFastWritableBytes
in classByteBuf
-
capacity
Description copied from class:ByteBuf
Adjusts the capacity of this buffer. If thenewCapacity
is less than the current capacity, the content of this buffer is truncated. If thenewCapacity
is greater than the current capacity, the buffer is appended with unspecified data whose length is(newCapacity - currentCapacity)
. -
alloc
Description copied from class:ByteBuf
Returns theByteBufAllocator
which created this buffer. -
order
Description copied from class:ByteBuf
Returns the endianness of this buffer. -
unwrap
Description copied from class:ByteBuf
Return the underlying buffer instance if this buffer is a wrapper of another buffer. -
retainedDuplicate
Description copied from class:ByteBuf
Returns a retained buffer which shares the whole region of this buffer. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method is identical tobuf.slice(0, buf.capacity())
. This method does not modifyreaderIndex
orwriterIndex
of this buffer.Note that this method returns a retained buffer unlike
ByteBuf.slice(int, int)
. This method behaves similarly toduplicate().retain()
except that this method may return a buffer implementation that produces less garbage.- Overrides:
retainedDuplicate
in classAbstractByteBuf
-
retainedSlice
Description copied from class:ByteBuf
Returns a retained slice of this buffer's readable bytes. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method is identical tobuf.slice(buf.readerIndex(), buf.readableBytes())
. This method does not modifyreaderIndex
orwriterIndex
of this buffer.Note that this method returns a retained buffer unlike
ByteBuf.slice()
. This method behaves similarly toslice().retain()
except that this method may return a buffer implementation that produces less garbage.- Overrides:
retainedSlice
in classAbstractByteBuf
-
retainedSlice
Description copied from class:ByteBuf
Returns a retained slice of this buffer's sub-region. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method does not modifyreaderIndex
orwriterIndex
of this buffer.Note that this method returns a retained buffer unlike
ByteBuf.slice(int, int)
. This method behaves similarly toslice(...).retain()
except that this method may return a buffer implementation that produces less garbage.- Overrides:
retainedSlice
in classAbstractByteBuf
-
internalNioBuffer
-
newInternalNioBuffer
-
deallocate
protected final void deallocate()Description copied from class:AbstractReferenceCountedByteBuf
Called onceAbstractReferenceCountedByteBuf.refCnt()
is equals 0.- Specified by:
deallocate
in classAbstractReferenceCountedByteBuf
-
idx
protected final int idx(int index) -
_internalNioBuffer
-
duplicateInternalNioBuffer
-
internalNioBuffer
Description copied from class:ByteBuf
Internal use only: Exposes the internal NIO buffer.- Specified by:
internalNioBuffer
in classByteBuf
-
nioBufferCount
public final int nioBufferCount()Description copied from class:ByteBuf
Returns the maximum number of NIOByteBuffer
s that consist this buffer. Note thatByteBuf.nioBuffers()
orByteBuf.nioBuffers(int, int)
might return a less number ofByteBuffer
s.- Specified by:
nioBufferCount
in classByteBuf
- Returns:
-1
if this buffer has no underlyingByteBuffer
. the number of the underlyingByteBuffer
s if this buffer has at least one underlyingByteBuffer
. Note that this method does not return0
to avoid confusion.- See Also:
-
nioBuffer
Description copied from class:ByteBuf
Exposes this buffer's sub-region as an NIOByteBuffer
. The returned buffer either share or contains the copied content of this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method does not modifyreaderIndex
orwriterIndex
of this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer and it adjusted its capacity. -
nioBuffers
Description copied from class:ByteBuf
Exposes this buffer's bytes as an NIOByteBuffer
's for the specified index and length The returned buffer either share or contains the copied content of this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method does not modifyreaderIndex
orwriterIndex
of this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer and it adjusted its capacity.- Specified by:
nioBuffers
in classByteBuf
- See Also:
-
isContiguous
public final boolean isContiguous()Description copied from class:ByteBuf
Returnstrue
if thisByteBuf
implementation is backed by a single memory region. Composite buffer implementations must return false even if they currently hold ≤ 1 components. For buffers that returntrue
, it's guaranteed that a successful call toByteBuf.discardReadBytes()
will increase the value ofByteBuf.maxFastWritableBytes()
by the currentreaderIndex
.This method will return
false
by default, and afalse
return value does not necessarily mean that the implementation is composite or that it is not backed by a single memory region.- Overrides:
isContiguous
in classByteBuf
-
getBytes
Description copied from class:ByteBuf
Transfers this buffer's data to the specified channel starting at the specified absoluteindex
. This method does not modifyreaderIndex
orwriterIndex
of this buffer.- Specified by:
getBytes
in classByteBuf
- Parameters:
length
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes written out to the specified channel
- Throws:
IOException
- if the specified channel threw an exception during I/O
-
readBytes
Description copied from class:ByteBuf
Transfers this buffer's data to the specified stream starting at the currentreaderIndex
.- Overrides:
readBytes
in classAbstractByteBuf
- Parameters:
length
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes written out to the specified channel
- Throws:
IOException
- if the specified channel threw an exception during I/O
-
getBytes
Description copied from class:ByteBuf
Transfers this buffer's data starting at the specified absoluteindex
to the specified channel starting at the given file position. This method does not modifyreaderIndex
orwriterIndex
of this buffer. This method does not modify the channel's position.- Specified by:
getBytes
in classByteBuf
- Parameters:
position
- the file position at which the transfer is to beginlength
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes written out to the specified channel
- Throws:
IOException
- if the specified channel threw an exception during I/O
-
readBytes
Description copied from class:ByteBuf
Transfers this buffer's data starting at the currentreaderIndex
to the specified channel starting at the given file position. This method does not modify the channel's position.- Overrides:
readBytes
in classAbstractByteBuf
- Parameters:
position
- the file position at which the transfer is to beginlength
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes written out to the specified channel
- Throws:
IOException
- if the specified channel threw an exception during I/O
-
setBytes
Description copied from class:ByteBuf
Transfers the content of the specified source channel to this buffer starting at the specified absoluteindex
. This method does not modifyreaderIndex
orwriterIndex
of this buffer.- Specified by:
setBytes
in classByteBuf
- Parameters:
length
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified channel.
-1
if the specified channel is closed or it reached EOF. - Throws:
IOException
- if the specified channel threw an exception during I/O
-
setBytes
Description copied from class:ByteBuf
Transfers the content of the specified source channel starting at the given file position to this buffer starting at the specified absoluteindex
. This method does not modifyreaderIndex
orwriterIndex
of this buffer. This method does not modify the channel's position.- Specified by:
setBytes
in classByteBuf
- Parameters:
position
- the file position at which the transfer is to beginlength
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified channel.
-1
if the specified channel is closed or it reached EOF. - Throws:
IOException
- if the specified channel threw an exception during I/O
-