diff --git a/transport/src/main/java/io/netty/channel/AbstractChannel.java b/transport/src/main/java/io/netty/channel/AbstractChannel.java index 250e4efd54..3449402ab8 100644 --- a/transport/src/main/java/io/netty/channel/AbstractChannel.java +++ b/transport/src/main/java/io/netty/channel/AbstractChannel.java @@ -107,6 +107,22 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha return buf != null && buf.isWritable(); } + @Override + public long bytesBeforeUnwritable() { + ChannelOutboundBuffer buf = unsafe.outboundBuffer(); + // isWritable() is currently assuming if there is no outboundBuffer then the channel is not writable. + // We should be consistent with that here. + return buf != null ? buf.bytesBeforeUnwritable() : 0; + } + + @Override + public long bytesBeforeWritable() { + ChannelOutboundBuffer buf = unsafe.outboundBuffer(); + // isWritable() is currently assuming if there is no outboundBuffer then the channel is not writable. + // We should be consistent with that here. + return buf != null ? buf.bytesBeforeWritable() : Long.MAX_VALUE; + } + @Override public Channel parent() { return parent; diff --git a/transport/src/main/java/io/netty/channel/Channel.java b/transport/src/main/java/io/netty/channel/Channel.java index cecf93d3b9..5f4f0405e7 100644 --- a/transport/src/main/java/io/netty/channel/Channel.java +++ b/transport/src/main/java/io/netty/channel/Channel.java @@ -163,6 +163,18 @@ public interface Channel extends AttributeMap, Comparable { */ boolean isWritable(); + /** + * Get how many bytes can be written until {@link #isWritable()} returns {@code false}. + * This quantity will always be non-negative. If {@link #isWritable()} is {@code false} then 0. + */ + long bytesBeforeUnwritable(); + + /** + * Get how many bytes must be drained from underlying buffers until {@link #isWritable()} returns {@code true}. + * This quantity will always be non-negative. If {@link #isWritable()} is {@code true} then 0. + */ + long bytesBeforeWritable(); + /** * Returns an internal-use-only object that provides unsafe operations. */ diff --git a/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java b/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java index b3a0a42202..7afd34c514 100644 --- a/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java +++ b/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java @@ -693,7 +693,7 @@ public final class ChannelOutboundBuffer { * Get how many bytes can be written until {@link #isWritable()} returns {@code false}. * This quantity will always be non-negative. If {@link #isWritable()} is {@code false} then 0. */ - public long bytesBeforeUnWritable() { + public long bytesBeforeUnwritable() { long bytes = channel.config().getWriteBufferHighWaterMark() - totalPendingSize; // If bytes is negative we know we are not writable, but if bytes is non-negative we have to check writability. // Note that totalPendingSize and isWritable() use different volatile variables that are not synchronized