diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java index e1bbf63166..6366340c63 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageDecoder.java @@ -89,13 +89,10 @@ public abstract class ByteToMessageDecoder callDecode(ctx, in); } decodeLast(ctx, in, out); - - } catch (Throwable t) { - if (t instanceof CodecException) { - throw (CodecException) t; - } else { - throw new DecoderException(t); - } + } catch (CodecException e) { + throw e; + } catch (Throwable cause) { + throw new DecoderException(cause); } finally { if (out.drainToNextInbound(ctx)) { ctx.fireInboundBufferUpdated(); @@ -131,12 +128,10 @@ public abstract class ByteToMessageDecoder break; } } - } catch (Throwable t) { - if (t instanceof CodecException) { - throw (CodecException) t; - } else { - throw new DecoderException(t); - } + } catch (CodecException e) { + throw e; + } catch (Throwable cause) { + throw new DecoderException(cause); } finally { if (out.drainToNextInbound(ctx)) { decodeWasNull = false; diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java index 4efa56ded3..91d24eb4da 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java @@ -52,6 +52,10 @@ public abstract class MessageToMessageDecoder extends ChannelInboundMessageHa OutputMessageBuf out = OutputMessageBuf.get(); try { decode(ctx, msg, out); + } catch (CodecException e) { + throw e; + } catch (Throwable cause) { + throw new DecoderException(cause); } finally { out.drainToNextInbound(ctx); } diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java index ab212d0618..4195ba9c3a 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java @@ -53,11 +53,7 @@ public abstract class MessageToMessageEncoder extends ChannelOutboundMessageH } catch (CodecException e) { throw e; } catch (Throwable cause) { - if (cause instanceof CodecException) { - throw (CodecException) cause; - } else { - throw new EncoderException(cause); - } + throw new EncoderException(cause); } finally { out.drainToNextOutbound(ctx); } diff --git a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java index a69a520433..ec3429ee00 100644 --- a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java @@ -375,12 +375,10 @@ public abstract class ReplayingDecoder extends ByteToMessageDecoder { } catch (Signal replay) { // Ignore replay.expect(REPLAY); - } catch (Throwable t) { - if (t instanceof CodecException) { - throw (CodecException) t; - } else { - throw new DecoderException(t); - } + } catch (CodecException e) { + throw e; + } catch (Throwable cause) { + throw new DecoderException(cause); } finally { if (out.drainToNextInbound(ctx)) { ctx.fireInboundBufferUpdated(); @@ -397,51 +395,47 @@ public abstract class ReplayingDecoder extends ByteToMessageDecoder { OutputMessageBuf out = OutputMessageBuf.get(); try { while (in.isReadable()) { + int oldReaderIndex = checkpoint = in.readerIndex(); + int outSize = out.size(); + S oldState = state; try { - int oldReaderIndex = checkpoint = in.readerIndex(); - int outSize = out.size(); - S oldState = state; - try { - decode(ctx, replayable, out); - if (outSize == out.size()) { - wasNull = true; - if (oldReaderIndex == in.readerIndex() && oldState == state) { - throw new IllegalStateException( - "null cannot be returned if no data is consumed and state didn't change."); - } else { - // Previous data has been discarded or caused state transition. - // Probably it is reading on. - continue; - } - } - } catch (Signal replay) { - replay.expect(REPLAY); - // Return to the checkpoint (or oldPosition) and retry. - int checkpoint = this.checkpoint; - if (checkpoint >= 0) { - in.readerIndex(checkpoint); + decode(ctx, replayable, out); + if (outSize == out.size()) { + wasNull = true; + if (oldReaderIndex == in.readerIndex() && oldState == state) { + throw new IllegalStateException( + "null cannot be returned if no data is consumed and state didn't change."); } else { - // Called by cleanup() - no need to maintain the readerIndex - // anymore because the buffer has been released already. + // Previous data has been discarded or caused state transition. + // Probably it is reading on. + continue; } - break; } - wasNull = false; - - if (oldReaderIndex == in.readerIndex() && oldState == state) { - throw new IllegalStateException( - "decode() method must consume at least one byte " + - "if it returned a decoded message (caused by: " + - getClass() + ')'); - } - } catch (Throwable t) { - if (t instanceof CodecException) { - throw (CodecException) t; + } catch (Signal replay) { + replay.expect(REPLAY); + // Return to the checkpoint (or oldPosition) and retry. + int checkpoint = this.checkpoint; + if (checkpoint >= 0) { + in.readerIndex(checkpoint); } else { - throw new DecoderException(t); + // Called by cleanup() - no need to maintain the readerIndex + // anymore because the buffer has been released already. } + break; + } + wasNull = false; + + if (oldReaderIndex == in.readerIndex() && oldState == state) { + throw new IllegalStateException( + "decode() method must consume at least one byte " + + "if it returned a decoded message (caused by: " + + getClass() + ')'); } } + } catch (CodecException e) { + throw e; + } catch (Throwable cause) { + throw new DecoderException(cause); } finally { if (out.drainToNextInbound(ctx)) { decodeWasNull = false; @@ -465,5 +459,4 @@ public abstract class ReplayingDecoder extends ByteToMessageDecoder { super.channelReadSuspended(ctx); } - }