public final class CoalescingBufferQueue
extends java.lang.Object
ByteBuf
and consumers take bytes in
arbitrary lengths. This allows producers to add lots of small buffers and the consumer to take all the bytes
out in a single buffer. Conversely the producer may add larger buffers and the consumer could take the bytes in
many small buffers.
Bytes are added and removed with promises. If the last byte of a buffer added with a promise is removed then
that promise will complete when the promise passed to remove(int, io.netty.channel.ChannelPromise)
completes.
This functionality is useful for aggregating or partitioning writes into fixed size buffers for framing protocols such as HTTP2.
Modifier and Type | Field and Description |
---|---|
private java.util.ArrayDeque<java.lang.Object> |
bufAndListenerPairs |
private Channel |
channel |
private int |
readableBytes |
Constructor and Description |
---|
CoalescingBufferQueue(Channel channel) |
CoalescingBufferQueue(Channel channel,
int initSize) |
Modifier and Type | Method and Description |
---|---|
void |
add(ByteBuf buf)
Add a buffer to the end of the queue.
|
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.
|
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 buffers bytes have been consumed from the queue and written.
|
private ByteBuf |
compose(ByteBuf current,
ByteBuf next)
Compose the current buffer with another.
|
void |
copyTo(CoalescingBufferQueue dest)
Copy all pending entries in this queue into the destination queue.
|
boolean |
isEmpty()
Are there pending buffers in the queue.
|
int |
readableBytes()
The number of readable bytes.
|
private void |
releaseAndCompleteAll(ChannelFuture future) |
void |
releaseAndFailAll(java.lang.Throwable cause)
Release all buffers in the queue and complete all listeners and promises.
|
ByteBuf |
remove(int bytes,
ChannelPromise aggregatePromise)
Remove a
ByteBuf from the queue with the specified number of bytes. |
private final Channel channel
private final java.util.ArrayDeque<java.lang.Object> bufAndListenerPairs
private int readableBytes
public CoalescingBufferQueue(Channel channel)
public CoalescingBufferQueue(Channel channel, int initSize)
public void add(ByteBuf buf)
public void add(ByteBuf buf, ChannelPromise promise)
buf
- to add to the tail of the queuepromise
- to complete when all the bytes have been consumed and written, can be void.public void add(ByteBuf buf, ChannelFutureListener listener)
buf
- to add to the tail of the queuelistener
- to notify when all the bytes have been consumed and written, can be null
.public ByteBuf remove(int bytes, ChannelPromise aggregatePromise)
ByteBuf
from 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 aggregate ChannelPromise
completes.bytes
- the maximum number of readable bytes in the returned ByteBuf
, if bytes
is greater
than readableBytes
then a buffer of length readableBytes
is returned.aggregatePromise
- used to aggregate the promises and listeners for the constituent buffers.ByteBuf
composed of the enqueued buffers.private ByteBuf compose(ByteBuf current, ByteBuf next)
public int readableBytes()
public boolean isEmpty()
public void releaseAndFailAll(java.lang.Throwable cause)
private void releaseAndCompleteAll(ChannelFuture future)
public void copyTo(CoalescingBufferQueue dest)
dest
- to copy pending buffers to.