From 352e36a179ce12817551d81ad2861eef46a10e4b Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 18 Mar 2018 13:42:00 +0100 Subject: [PATCH] Remove code duplication in ChunkedWriteHandler Motivation: We had some code duplication in ChunkedWriteHandler. Modifications: Factor out duplicated code into private methods and reuse it. Result: Less code duplication. --- .../handler/stream/ChunkedWriteHandler.java | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java b/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java index bbeec19585..1bc0c6bf81 100644 --- a/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java +++ b/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java @@ -102,31 +102,29 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler { return; } if (ctx.executor().inEventLoop()) { - try { - doFlush(ctx); - } catch (Exception e) { - if (logger.isWarnEnabled()) { - logger.warn("Unexpected exception while sending chunks.", e); - } - } + resumeTransfer0(ctx); } else { // let the transfer resume on the next event loop round ctx.executor().execute(new Runnable() { @Override public void run() { - try { - doFlush(ctx); - } catch (Exception e) { - if (logger.isWarnEnabled()) { - logger.warn("Unexpected exception while sending chunks.", e); - } - } + resumeTransfer0(ctx); } }); } } + private void resumeTransfer0(ChannelHandlerContext ctx) { + try { + doFlush(ctx); + } catch (Exception e) { + if (logger.isWarnEnabled()) { + logger.warn("Unexpected exception while sending chunks.", e); + } + } + } + @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { queue.add(new PendingWrite(msg, promise)); @@ -192,7 +190,7 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler { } } - private void doFlush(final ChannelHandlerContext ctx) throws Exception { + private void doFlush(final ChannelHandlerContext ctx) { final Channel channel = ctx.channel(); if (!channel.isActive()) { discard(null); @@ -317,7 +315,7 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler { } } - static void closeInput(ChunkedInput chunks) { + private static void closeInput(ChunkedInput chunks) { try { chunks.close(); } catch (Throwable t) { @@ -346,12 +344,7 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler { // No need to notify the progress or fulfill the promise because it's done already. return; } - - if (promise instanceof ChannelProgressivePromise) { - // Now we know what the total is. - ((ChannelProgressivePromise) promise).tryProgress(total, total); - } - + progress(total, total); promise.trySuccess(); }