Correctly handle forward of remaining data on removal

This commit is contained in:
Norman Maurer 2013-07-09 22:41:56 +02:00
parent 488ffb11d8
commit 768152cf88

View File

@ -101,9 +101,13 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
@Override @Override
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception { public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = internalBuffer(); ByteBuf buf = internalBuffer();
int readable = buf.readableBytes();
if (buf.isReadable()) { if (buf.isReadable()) {
ctx.fireChannelRead(buf); ByteBuf bytes = buf.readBytes(readable);
buf.release();
ctx.fireChannelRead(bytes);
} }
cumulation = null;
ctx.fireChannelReadComplete(); ctx.fireChannelReadComplete();
handlerRemoved0(ctx); handlerRemoved0(ctx);
} }
@ -123,11 +127,11 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
if (cumulation == null) { if (cumulation == null) {
cumulation = data; cumulation = data;
try { try {
callDecode(ctx, data, out); callDecode(ctx, cumulation, out);
} finally { } finally {
if (!data.isReadable()) { if (cumulation != null && !cumulation.isReadable()) {
cumulation.release();
cumulation = null; cumulation = null;
data.release();
} }
} }
} else { } else {
@ -141,11 +145,13 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
cumulation.writeBytes(data); cumulation.writeBytes(data);
callDecode(ctx, cumulation, out); callDecode(ctx, cumulation, out);
} finally { } finally {
if (!cumulation.isReadable()) { if (cumulation != null) {
cumulation.release(); if (!cumulation.isReadable()) {
cumulation = null; cumulation.release();
} else { cumulation = null;
cumulation.discardSomeReadBytes(); } else {
cumulation.discardSomeReadBytes();
}
} }
data.release(); data.release();
} }