Package io.netty.channel
Class AbstractCoalescingBufferQueue
- java.lang.Object
-
- io.netty.channel.AbstractCoalescingBufferQueue
-
- Direct Known Subclasses:
CoalescingBufferQueue,SslHandler.SslHandlerCoalescingBufferQueue
@UnstableApi public abstract class AbstractCoalescingBufferQueue extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.ArrayDeque<java.lang.Object>bufAndListenerPairsprivate static InternalLoggerloggerprivate intreadableBytesprivate PendingBytesTrackertracker
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractCoalescingBufferQueue(Channel channel, int initSize)Create a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidadd(ByteBuf buf)Add a buffer to the end of the queue.voidadd(ByteBuf buf, ChannelFutureListener listener)Add a buffer to the end of the queue and associate a listener with it that should be completed when all the buffers bytes have been consumed from the queue and written.voidadd(ByteBuf buf, ChannelPromise promise)Add a buffer to the end of the queue and associate a promise with it that should be completed when all the buffer's bytes have been consumed from the queue and written.private voidaddFirst(ByteBuf buf, ChannelFutureListener listener)voidaddFirst(ByteBuf buf, ChannelPromise promise)Add a buffer to the front of the queue and associate a promise with it that should be completed when all the buffer's bytes have been consumed from the queue and written.protected abstract ByteBufcompose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next)Calculate the result ofcurrent + next.protected ByteBufcomposeFirst(ByteBufAllocator allocator, ByteBuf first)Calculate the firstByteBufwhich will be used in subsequent calls tocompose(ByteBufAllocator, ByteBuf, ByteBuf).protected ByteBufcomposeIntoComposite(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next)protected ByteBufcopyAndCompose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next)voidcopyTo(AbstractCoalescingBufferQueue dest)Copy all pending entries in this queue into the destination queue.private voiddecrementReadableBytes(int decrement)private voidincrementReadableBytes(int increment)booleanisEmpty()Are there pending buffers in the queue.intreadableBytes()The number of readable bytes.private voidreleaseAndCompleteAll(ChannelFuture future)voidreleaseAndFailAll(ChannelOutboundInvoker invoker, java.lang.Throwable cause)Release all buffers in the queue and complete all listeners and promises.ByteBufremove(ByteBufAllocator alloc, int bytes, ChannelPromise aggregatePromise)Remove aByteBuffrom the queue with the specified number of bytes.protected abstract ByteBufremoveEmptyValue()The value to return whenremove(ByteBufAllocator, int, ChannelPromise)is called but the queue is empty.ByteBufremoveFirst(ChannelPromise aggregatePromise)Remove the firstByteBuffrom the queue.protected intsize()Get the number of elements in this queue added via one of theadd(ByteBuf)methods.private static ChannelFutureListenertoChannelFutureListener(ChannelPromise promise)voidwriteAndRemoveAll(ChannelHandlerContext ctx)Writes all remaining elements in this queue.
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
bufAndListenerPairs
private final java.util.ArrayDeque<java.lang.Object> bufAndListenerPairs
-
tracker
private final PendingBytesTracker tracker
-
readableBytes
private int readableBytes
-
-
Constructor Detail
-
AbstractCoalescingBufferQueue
protected AbstractCoalescingBufferQueue(Channel channel, int initSize)
Create a new instance.- Parameters:
channel- theChannelwhich will have theChannel.isWritable()reflect the amount of queued buffers ornullif there is no writability state updated.initSize- the initial size of the underlying queue.
-
-
Method Detail
-
addFirst
public final void addFirst(ByteBuf buf, ChannelPromise promise)
Add a buffer to the front of the queue and associate a promise with it that should be completed when all the buffer's bytes have been consumed from the queue and written.- Parameters:
buf- to add to the head of the queuepromise- to complete when all the bytes have been consumed and written, can be void.
-
addFirst
private void addFirst(ByteBuf buf, ChannelFutureListener listener)
-
add
public final void add(ByteBuf buf)
Add a buffer to the end of the queue.
-
add
public final void add(ByteBuf buf, ChannelPromise promise)
Add a buffer to the end of the queue and associate a promise with it that should be completed when all the buffer's bytes have been consumed from the queue and written.- Parameters:
buf- to add to the tail of the queuepromise- to complete when all the bytes have been consumed and written, can be void.
-
add
public final void add(ByteBuf buf, ChannelFutureListener listener)
Add a buffer to the end of the queue and associate a listener with it that should be completed when all the buffers bytes have been consumed from the queue and written.- Parameters:
buf- to add to the tail of the queuelistener- to notify when all the bytes have been consumed and written, can benull.
-
removeFirst
public final ByteBuf removeFirst(ChannelPromise aggregatePromise)
Remove the firstByteBuffrom the queue.- Parameters:
aggregatePromise- used to aggregate the promises and listeners for the returned buffer.- Returns:
- the first
ByteBuffrom the queue.
-
remove
public final ByteBuf remove(ByteBufAllocator alloc, int bytes, ChannelPromise aggregatePromise)
Remove aByteBuffrom the queue with the specified number of bytes. Any added buffer who's bytes are fully consumed during removal will have it's promise completed when the passed aggregateChannelPromisecompletes.- Parameters:
alloc- The allocator used if a newByteBufis generated during the aggregation process.bytes- the maximum number of readable bytes in the returnedByteBuf, ifbytesis greater thanreadableBytesthen a buffer of lengthreadableBytesis returned.aggregatePromise- used to aggregate the promises and listeners for the constituent buffers.- Returns:
- a
ByteBufcomposed of the enqueued buffers.
-
readableBytes
public final int readableBytes()
The number of readable bytes.
-
isEmpty
public final boolean isEmpty()
Are there pending buffers in the queue.
-
releaseAndFailAll
public final void releaseAndFailAll(ChannelOutboundInvoker invoker, java.lang.Throwable cause)
Release all buffers in the queue and complete all listeners and promises.
-
copyTo
public final void copyTo(AbstractCoalescingBufferQueue dest)
Copy all pending entries in this queue into the destination queue.- Parameters:
dest- to copy pending buffers to.
-
writeAndRemoveAll
public final void writeAndRemoveAll(ChannelHandlerContext ctx)
Writes all remaining elements in this queue.- Parameters:
ctx- The context to write all elements to.
-
compose
protected abstract ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next)
Calculate the result ofcurrent + next.
-
composeIntoComposite
protected final ByteBuf composeIntoComposite(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next)
-
copyAndCompose
protected final ByteBuf copyAndCompose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next)
- Parameters:
alloc- The allocator to use to allocate the new buffer.cumulation- The current cumulation.next- The next buffer.- Returns:
- The result of
cumulation + next.
-
composeFirst
protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first)
Calculate the firstByteBufwhich will be used in subsequent calls tocompose(ByteBufAllocator, ByteBuf, ByteBuf).
-
removeEmptyValue
protected abstract ByteBuf removeEmptyValue()
The value to return whenremove(ByteBufAllocator, int, ChannelPromise)is called but the queue is empty.- Returns:
- the
ByteBufwhich represents an empty queue.
-
size
protected final int size()
Get the number of elements in this queue added via one of theadd(ByteBuf)methods.- Returns:
- the number of elements in this queue.
-
releaseAndCompleteAll
private void releaseAndCompleteAll(ChannelFuture future)
-
incrementReadableBytes
private void incrementReadableBytes(int increment)
-
decrementReadableBytes
private void decrementReadableBytes(int decrement)
-
toChannelFutureListener
private static ChannelFutureListener toChannelFutureListener(ChannelPromise promise)
-
-