diff --git a/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java b/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java index 9ce6701d3c..605f3572a2 100755 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java @@ -134,12 +134,6 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel { @Override protected void doFlushByteBuffer(ByteBuf buf) throws Exception { - if (!buf.isReadable()) { - // Reset reader/writerIndex to 0 if the buffer is empty. - buf.clear(); - return; - } - for (int i = config().getWriteSpinCount() - 1; i >= 0; i --) { int localFlushedAmount = doWriteBytes(buf, i == 0); if (localFlushedAmount > 0) { diff --git a/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java b/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java index ad10299b40..3442dec0de 100644 --- a/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java +++ b/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java @@ -458,17 +458,15 @@ public final class NioEventLoop extends SingleThreadEventLoop { } private static void processWritable(SelectionKey k, AbstractNioChannel ch) { - if (ch.writableTasks.isEmpty()) { - ch.unsafe().flushNow(); - } else { - NioTask task; - for (;;) { - task = ch.writableTasks.poll(); - if (task == null) { break; } - processSelectedKey(ch.selectionKey(), task); - } - k.interestOps(k.interestOps() | SelectionKey.OP_WRITE); + NioTask task; + for (;;) { + task = ch.writableTasks.poll(); + if (task == null) { break; } + processSelectedKey(ch.selectionKey(), task); } + + // Call flushNow which will also take care of clear the OP_WRITE once there is nothing left to write + ch.unsafe().flushNow(); } private static void unregisterWritableTasks(AbstractNioChannel ch) {