diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java index 018a68e743..a2ad13ead7 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToByteDecoder.java @@ -82,19 +82,16 @@ public abstract class ByteToByteDecoder extends ChannelInboundByteHandlerAdapter int oldOutSize = out.readableBytes(); try { decodeLast(ctx, in, out); + } catch (CodecException e) { + throw e; } catch (Throwable t) { - if (t instanceof CodecException) { - ctx.fireExceptionCaught(t); - } else { - ctx.fireExceptionCaught(new DecoderException(t)); + throw new DecoderException(t); + } finally { + if (out.readableBytes() > oldOutSize) { + ctx.fireInboundBufferUpdated(); } + ctx.fireChannelInactive(); } - - if (out.readableBytes() > oldOutSize) { - ctx.fireInboundBufferUpdated(); - } - - ctx.fireChannelInactive(); } /** @@ -102,25 +99,23 @@ public abstract class ByteToByteDecoder extends ChannelInboundByteHandlerAdapter */ private void callDecode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) { int oldOutSize = out.readableBytes(); - while (in.isReadable()) { - int oldInSize = in.readableBytes(); - try { - decode(ctx, in, out); - } catch (Throwable t) { - if (t instanceof CodecException) { - ctx.fireExceptionCaught(t); - } else { - ctx.fireExceptionCaught(new DecoderException(t)); + try { + while (in.isReadable()) { + int oldInSize = in.readableBytes(); + decode(ctx, in, out); + if (oldInSize == in.readableBytes() || isSingleDecode()) { + break; } } - if (oldInSize == in.readableBytes() || isSingleDecode()) { - break; + } catch (CodecException e) { + throw e; + } catch (Throwable t) { + throw new DecoderException(t); + } finally { + if (out.readableBytes() > oldOutSize) { + ctx.fireInboundBufferUpdated(); } } - - if (out.readableBytes() > oldOutSize) { - ctx.fireInboundBufferUpdated(); - } } /** diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java index 78a3018bdc..f1ed0f9212 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToByteEncoder.java @@ -57,28 +57,25 @@ public abstract class ByteToByteEncoder extends ChannelOutboundByteHandlerAdapte ByteBuf out = ctx.nextOutboundByteBuffer(); boolean encoded = false; - while (in.isReadable()) { - int oldInSize = in.readableBytes(); - try { + try { + while (in.isReadable()) { + int oldInSize = in.readableBytes(); encode(ctx, in, out); encoded = true; - } catch (Throwable t) { - Throwable cause; - if (t instanceof CodecException) { - cause = t; - } else { - cause = new EncoderException(t); + if (oldInSize == in.readableBytes()) { + break; } - if (encoded) { - cause = new IncompleteFlushException("unable to encode all bytes", cause); - } - in.discardSomeReadBytes(); - promise.setFailure(cause); - return; } - if (oldInSize == in.readableBytes()) { - break; + } catch (Throwable t) { + if (!(t instanceof CodecException)) { + t = new EncoderException(t); } + if (encoded) { + t = new IncompleteFlushException("unable to encode all bytes", t); + } + in.discardSomeReadBytes(); + promise.setFailure(t); + return; } ctx.flush(promise);