From 936e5ae1a8bfac4679ade6049aed80ea08de06df Mon Sep 17 00:00:00 2001 From: norman Date: Fri, 2 Dec 2011 07:22:14 +0100 Subject: [PATCH] Make sure the cumulation Buffer is only created if really needed. See #88 --- .../handler/codec/frame/FrameDecoder.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jboss/netty/handler/codec/frame/FrameDecoder.java b/src/main/java/org/jboss/netty/handler/codec/frame/FrameDecoder.java index a3e173d168..aebd01ca68 100644 --- a/src/main/java/org/jboss/netty/handler/codec/frame/FrameDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/frame/FrameDecoder.java @@ -207,17 +207,28 @@ public abstract class FrameDecoder extends SimpleChannelUpstreamHandler { return; } - ChannelBuffer cumulation = cumulation(ctx); - if (cumulation.readable()) { - cumulation.discardReadBytes(); - cumulation.writeBytes(input); - callDecode(ctx, e.getChannel(), cumulation, e.getRemoteAddress()); - } else { + if (cumulation == null) { + // the cumulation buffer is not created yet so just pass the input to callDecode(...) method callDecode(ctx, e.getChannel(), input, e.getRemoteAddress()); if (input.readable()) { + // seems like there is something readable left in the input buffer. So create the cumulation buffer and copy the input into it + ChannelBuffer cumulation = cumulation(ctx); cumulation.writeBytes(input); } + } else { + ChannelBuffer cumulation = cumulation(ctx); + if (cumulation.readable()) { + cumulation.discardReadBytes(); + cumulation.writeBytes(input); + callDecode(ctx, e.getChannel(), cumulation, e.getRemoteAddress()); + } else { + callDecode(ctx, e.getChannel(), input, e.getRemoteAddress()); + if (input.readable()) { + cumulation.writeBytes(input); + } + } } + } @Override @@ -349,6 +360,12 @@ public abstract class FrameDecoder extends SimpleChannelUpstreamHandler { } } + /** + * Get the currently used {@link ChannelBuffer} for cumulation or create one in a lazy fashion if none exist yet + * + * @param ctx the {@link ChannelHandlerContext} for this handler + * @return buffer the {@link ChannelBuffer} which is used fo cumulation + */ private ChannelBuffer cumulation(ChannelHandlerContext ctx) { ChannelBuffer c = cumulation; if (c == null) {