Class PooledByteBuf<T>
- java.lang.Object
-
- io.netty.buffer.ByteBuf
-
- io.netty.buffer.AbstractByteBuf
-
- io.netty.buffer.AbstractReferenceCountedByteBuf
-
- io.netty.buffer.PooledByteBuf<T>
-
- All Implemented Interfaces:
ReferenceCounted
,java.lang.Comparable<ByteBuf>
- Direct Known Subclasses:
PooledDirectByteBuf
,PooledHeapByteBuf
,PooledUnsafeDirectByteBuf
abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf
-
-
Field Summary
Fields Modifier and Type Field Description private ByteBufAllocator
allocator
(package private) PoolThreadCache
cache
protected PoolChunk<T>
chunk
protected long
handle
protected int
length
(package private) int
maxLength
protected T
memory
protected int
offset
private ObjectPool.Handle<PooledByteBuf<T>>
recyclerHandle
(package private) java.nio.ByteBuffer
tmpNioBuf
-
Fields inherited from class io.netty.buffer.AbstractByteBuf
checkAccessible, leakDetector, readerIndex, writerIndex
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
PooledByteBuf(ObjectPool.Handle<? extends PooledByteBuf<T>> recyclerHandle, int maxCapacity)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) java.nio.ByteBuffer
_internalNioBuffer(int index, int length, boolean duplicate)
ByteBufAllocator
alloc()
Returns theByteBufAllocator
which created this buffer.int
capacity()
Returns the number of bytes (octets) this buffer can contain.ByteBuf
capacity(int newCapacity)
Adjusts the capacity of this buffer.protected void
deallocate()
Called onceAbstractReferenceCountedByteBuf.refCnt()
is equals 0.(package private) java.nio.ByteBuffer
duplicateInternalNioBuffer(int index, int length)
int
getBytes(int index, java.nio.channels.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.int
getBytes(int index, java.nio.channels.GatheringByteChannel out, int length)
Transfers this buffer's data to the specified channel starting at the specified absoluteindex
.protected int
idx(int index)
(package private) void
init(PoolChunk<T> chunk, java.nio.ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength, PoolThreadCache cache)
private void
init0(PoolChunk<T> chunk, java.nio.ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength, PoolThreadCache cache)
(package private) void
initUnpooled(PoolChunk<T> chunk, int length)
protected java.nio.ByteBuffer
internalNioBuffer()
java.nio.ByteBuffer
internalNioBuffer(int index, int length)
Internal use only: Exposes the internal NIO buffer.boolean
isContiguous()
Returnstrue
if thisByteBuf
implementation is backed by a single memory region.int
maxFastWritableBytes()
Returns the maximum number of bytes which can be written for certain without involving an internal reallocation or data-copy.protected abstract java.nio.ByteBuffer
newInternalNioBuffer(T memory)
java.nio.ByteBuffer
nioBuffer(int index, int length)
Exposes this buffer's sub-region as an NIOByteBuffer
.int
nioBufferCount()
Returns the maximum number of NIOByteBuffer
s that consist this buffer.java.nio.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.java.nio.ByteOrder
order()
Returns the endianness of this buffer.int
readBytes(java.nio.channels.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.int
readBytes(java.nio.channels.GatheringByteChannel out, int length)
Transfers this buffer's data to the specified stream starting at the currentreaderIndex
.private void
recycle()
ByteBuf
retainedDuplicate()
Returns a retained buffer which shares the whole region of this buffer.ByteBuf
retainedSlice()
Returns a retained slice of this buffer's readable bytes.ByteBuf
retainedSlice(int index, int length)
Returns a retained slice of this buffer's sub-region.(package private) void
reuse(int maxCapacity)
Method must be called before reuse thisPooledByteBufAllocator
int
setBytes(int index, java.nio.channels.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
.int
setBytes(int index, java.nio.channels.ScatteringByteChannel in, int length)
Transfers the content of the specified source channel to this buffer starting at the specified absoluteindex
.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, copy, getBytes, getBytes, getBytes, getBytes, getDoubleLE, getFloatLE, hasArray, hasMemoryAddress, isDirect, memoryAddress, readDoubleLE, readFloatLE, setBytes, setBytes, setBytes, setBytes, setDoubleLE, setFloatLE, writeDoubleLE, writeFloatLE
-
-
-
-
Field Detail
-
recyclerHandle
private final ObjectPool.Handle<PooledByteBuf<T>> recyclerHandle
-
handle
protected long handle
-
memory
protected T memory
-
offset
protected int offset
-
length
protected int length
-
maxLength
int maxLength
-
cache
PoolThreadCache cache
-
tmpNioBuf
java.nio.ByteBuffer tmpNioBuf
-
allocator
private ByteBufAllocator allocator
-
-
Constructor Detail
-
PooledByteBuf
protected PooledByteBuf(ObjectPool.Handle<? extends PooledByteBuf<T>> recyclerHandle, int maxCapacity)
-
-
Method Detail
-
init
void init(PoolChunk<T> chunk, java.nio.ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength, PoolThreadCache cache)
-
init0
private void init0(PoolChunk<T> chunk, java.nio.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
public final ByteBuf capacity(int newCapacity)
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
public final ByteBufAllocator alloc()
Description copied from class:ByteBuf
Returns theByteBufAllocator
which created this buffer.
-
order
public final java.nio.ByteOrder order()
Description copied from class:ByteBuf
Returns the endianness of this buffer.
-
unwrap
public final ByteBuf unwrap()
Description copied from class:ByteBuf
Return the underlying buffer instance if this buffer is a wrapper of another buffer.
-
retainedDuplicate
public final ByteBuf 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
public final ByteBuf 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
public final ByteBuf retainedSlice(int index, int length)
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
protected final java.nio.ByteBuffer internalNioBuffer()
-
newInternalNioBuffer
protected abstract java.nio.ByteBuffer newInternalNioBuffer(T memory)
-
deallocate
protected final void deallocate()
Description copied from class:AbstractReferenceCountedByteBuf
Called onceAbstractReferenceCountedByteBuf.refCnt()
is equals 0.- Specified by:
deallocate
in classAbstractReferenceCountedByteBuf
-
recycle
private void recycle()
-
idx
protected final int idx(int index)
-
_internalNioBuffer
final java.nio.ByteBuffer _internalNioBuffer(int index, int length, boolean duplicate)
-
duplicateInternalNioBuffer
java.nio.ByteBuffer duplicateInternalNioBuffer(int index, int length)
-
internalNioBuffer
public final java.nio.ByteBuffer internalNioBuffer(int index, int length)
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:
ByteBuf.nioBuffer()
,ByteBuf.nioBuffer(int, int)
,ByteBuf.nioBuffers()
,ByteBuf.nioBuffers(int, int)
-
nioBuffer
public final java.nio.ByteBuffer nioBuffer(int index, int length)
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.- Specified by:
nioBuffer
in classByteBuf
- See Also:
ByteBuf.nioBufferCount()
,ByteBuf.nioBuffers()
,ByteBuf.nioBuffers(int, int)
-
nioBuffers
public final java.nio.ByteBuffer[] nioBuffers(int index, int length)
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:
ByteBuf.nioBufferCount()
,ByteBuf.nioBuffer()
,ByteBuf.nioBuffer(int, int)
-
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
public final int getBytes(int index, java.nio.channels.GatheringByteChannel out, int length) throws java.io.IOException
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.
-
readBytes
public final int readBytes(java.nio.channels.GatheringByteChannel out, int length) throws java.io.IOException
Description copied from class:ByteBuf
Transfers this buffer's data to the specified stream starting at the currentreaderIndex
.- Overrides:
readBytes
in classAbstractByteBuf
length
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes written out to the specified channel
- Throws:
java.io.IOException
- if the specified channel threw an exception during I/O
-
getBytes
public final int getBytes(int index, java.nio.channels.FileChannel out, long position, int length) throws java.io.IOException
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
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:
java.io.IOException
- if the specified channel threw an exception during I/O
-
readBytes
public final int readBytes(java.nio.channels.FileChannel out, long position, int length) throws java.io.IOException
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
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:
java.io.IOException
- if the specified channel threw an exception during I/O
-
setBytes
public final int setBytes(int index, java.nio.channels.ScatteringByteChannel in, int length) throws java.io.IOException
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.
-
setBytes
public final int setBytes(int index, java.nio.channels.FileChannel in, long position, int length) throws java.io.IOException
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
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. - Throws:
java.io.IOException
- if the specified channel threw an exception during I/O
-
-