Class ChannelOutboundBuffer

java.lang.Object
io.netty.channel.ChannelOutboundBuffer

public final class ChannelOutboundBuffer extends Object
(Transport implementors only) an internal data structure used by AbstractChannel to store its pending outbound write requests.

All methods must be called by a transport implementation from an I/O thread, except the following ones:

  • Field Details

  • Constructor Details

  • Method Details

    • addMessage

      public void addMessage(Object msg, int size, ChannelPromise promise)
      Add given message to this ChannelOutboundBuffer. The given ChannelPromise will be notified once the message was written.
    • addFlush

      public void addFlush()
      Add a flush to this ChannelOutboundBuffer. This means all previous added messages are marked as flushed and so you will be able to handle them.
    • incrementPendingOutboundBytes

      void incrementPendingOutboundBytes(long size)
      Increment the pending bytes which will be written at some point. This method is thread-safe!
    • incrementPendingOutboundBytes

      private void incrementPendingOutboundBytes(long size, boolean invokeLater)
    • decrementPendingOutboundBytes

      void decrementPendingOutboundBytes(long size)
      Decrement the pending bytes which will be written at some point. This method is thread-safe!
    • decrementPendingOutboundBytes

      private void decrementPendingOutboundBytes(long size, boolean invokeLater, boolean notifyWritability)
    • total

      private static long total(Object msg)
    • current

      public Object current()
      Return the current message to write or null if nothing was flushed before and so is ready to be written.
    • currentProgress

      public long currentProgress()
      Return the current message flush progress.
      Returns:
      0 if nothing was flushed before for the current message or there is no current message
    • progress

      public void progress(long amount)
      Notify the ChannelPromise of the current message about writing progress.
    • remove

      public boolean remove()
      Will remove the current message, mark its ChannelPromise as success and return true. If no flushed message exists at the time this method is called it will return false to signal that no more messages are ready to be handled.
    • remove

      public boolean remove(Throwable cause)
      Will remove the current message, mark its ChannelPromise as failure using the given Throwable and return true. If no flushed message exists at the time this method is called it will return false to signal that no more messages are ready to be handled.
    • remove0

      private boolean remove0(Throwable cause, boolean notifyWritability)
    • removeEntry

      private void removeEntry(ChannelOutboundBuffer.Entry e)
    • removeBytes

      public void removeBytes(long writtenBytes)
      Removes the fully written entries and update the reader index of the partially written entry. This operation assumes all messages in this buffer is ByteBuf.
    • clearNioBuffers

      private void clearNioBuffers()
    • nioBuffers

      public ByteBuffer[] nioBuffers()
      Returns an array of direct NIO buffers if the currently pending messages are made of ByteBuf only. nioBufferCount() and nioBufferSize() will return the number of NIO buffers in the returned array and the total number of readable bytes of the NIO buffers respectively.

      Note that the returned array is reused and thus should not escape AbstractChannel.doWrite(ChannelOutboundBuffer). Refer to NioSocketChannel.doWrite(ChannelOutboundBuffer) for an example.

    • nioBuffers

      public ByteBuffer[] nioBuffers(int maxCount, long maxBytes)
      Returns an array of direct NIO buffers if the currently pending messages are made of ByteBuf only. nioBufferCount() and nioBufferSize() will return the number of NIO buffers in the returned array and the total number of readable bytes of the NIO buffers respectively.

      Note that the returned array is reused and thus should not escape AbstractChannel.doWrite(ChannelOutboundBuffer). Refer to NioSocketChannel.doWrite(ChannelOutboundBuffer) for an example.

      Parameters:
      maxCount - The maximum amount of buffers that will be added to the return value.
      maxBytes - A hint toward the maximum number of bytes to include as part of the return value. Note that this value maybe exceeded because we make a best effort to include at least 1 ByteBuffer in the return value to ensure write progress is made.
    • nioBuffers

      private static int nioBuffers(ChannelOutboundBuffer.Entry entry, ByteBuf buf, ByteBuffer[] nioBuffers, int nioBufferCount, int maxCount)
    • expandNioBufferArray

      private static ByteBuffer[] expandNioBufferArray(ByteBuffer[] array, int neededSpace, int size)
    • nioBufferCount

      public int nioBufferCount()
      Returns the number of ByteBuffer that can be written out of the ByteBuffer array that was obtained via nioBuffers(). This method MUST be called after nioBuffers() was called.
    • nioBufferSize

      public long nioBufferSize()
      Returns the number of bytes that can be written out of the ByteBuffer array that was obtained via nioBuffers(). This method MUST be called after nioBuffers() was called.
    • isWritable

      public boolean isWritable()
      Returns true if and only if the total number of pending bytes did not exceed the write watermark of the Channel and no user-defined writability flag has been set to false.
    • getUserDefinedWritability

      public boolean getUserDefinedWritability(int index)
      Returns true if and only if the user-defined writability flag at the specified index is set to true.
    • setUserDefinedWritability

      public void setUserDefinedWritability(int index, boolean writable)
      Sets a user-defined writability flag at the specified index.
    • setUserDefinedWritability

      private void setUserDefinedWritability(int index)
    • clearUserDefinedWritability

      private void clearUserDefinedWritability(int index)
    • writabilityMask

      private static int writabilityMask(int index)
    • setWritable

      private void setWritable(boolean invokeLater)
    • setUnwritable

      private void setUnwritable(boolean invokeLater)
    • fireChannelWritabilityChanged

      private void fireChannelWritabilityChanged(boolean invokeLater)
    • size

      public int size()
      Returns the number of flushed messages in this ChannelOutboundBuffer.
    • isEmpty

      public boolean isEmpty()
      Returns true if there are flushed messages in this ChannelOutboundBuffer or false otherwise.
    • failFlushed

      void failFlushed(Throwable cause, boolean notify)
    • close

      void close(Throwable cause, boolean allowChannelOpen)
    • close

      void close(ClosedChannelException cause)
    • safeSuccess

      private static void safeSuccess(ChannelPromise promise)
    • safeFail

      private static void safeFail(ChannelPromise promise, Throwable cause)
    • recycle

      @Deprecated public void recycle()
      Deprecated.
    • totalPendingWriteBytes

      public long totalPendingWriteBytes()
    • bytesBeforeUnwritable

      public long bytesBeforeUnwritable()
      Get how many bytes can be written until isWritable() returns false. This quantity will always be non-negative. If isWritable() is false then 0.
    • bytesBeforeWritable

      public long bytesBeforeWritable()
      Get how many bytes must be drained from the underlying buffer until isWritable() returns true. This quantity will always be non-negative. If isWritable() is true then 0.
    • forEachFlushedMessage

      public void forEachFlushedMessage(ChannelOutboundBuffer.MessageProcessor processor) throws Exception
      Call ChannelOutboundBuffer.MessageProcessor.processMessage(Object) for each flushed message in this ChannelOutboundBuffer until ChannelOutboundBuffer.MessageProcessor.processMessage(Object) returns false or there are no more flushed messages to process.
      Throws:
      Exception
    • isFlushedEntry

      private boolean isFlushedEntry(ChannelOutboundBuffer.Entry e)