From b58a8f0106e65e3cab6126ee401c686c8234af45 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 9 Jan 2013 14:56:07 +0900 Subject: [PATCH] Add missing codec operations in ByteToMessageCodec --- .../handler/codec/ByteToMessageCodec.java | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java index 97f198e2ba..77b219b37d 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java @@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.MessageBuf; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelHandlerUtil; import io.netty.channel.ChannelInboundByteHandler; import io.netty.channel.ChannelOutboundMessageHandler; import io.netty.channel.ChannelPromise; @@ -27,11 +28,18 @@ public abstract class ByteToMessageCodec extends ChannelHandlerAdapter implements ChannelInboundByteHandler, ChannelOutboundMessageHandler { + private final Class[] encodableMessageTypes; private final MessageToByteEncoder encoder; private final ByteToMessageDecoder decoder; protected ByteToMessageCodec(Class... encodableMessageTypes) { - encoder = new MessageToByteEncoder(encodableMessageTypes) { + this.encodableMessageTypes = encodableMessageTypes; + encoder = new MessageToByteEncoder() { + @Override + public boolean isEncodable(Object msg) throws Exception { + return ByteToMessageCodec.this.isEncodable(msg); + } + @Override protected void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, ByteBuf out) throws Exception { ByteToMessageCodec.this.encode(ctx, msg, out); @@ -40,16 +48,30 @@ public abstract class ByteToMessageCodec decoder = new ByteToMessageDecoder() { @Override - public INBOUND_OUT decode( - ChannelHandlerContext ctx, ByteBuf in) throws Exception { + public INBOUND_OUT decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { return ByteToMessageCodec.this.decode(ctx, in); + + } + + @Override + protected INBOUND_OUT decodeLast(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + return ByteToMessageCodec.this.decodeLast(ctx, in); + } + + @Override + public void discardInboundReadBytes(ChannelHandlerContext ctx) throws Exception { + ByteToMessageCodec.this.discardInboundReadBytes(ctx); } }; } @Override - public ByteBuf newInboundBuffer( - ChannelHandlerContext ctx) throws Exception { + public void beforeAdd(ChannelHandlerContext ctx) throws Exception { + decoder.beforeAdd(ctx); + } + + @Override + public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception { return decoder.newInboundBuffer(ctx); } @@ -59,14 +81,12 @@ public abstract class ByteToMessageCodec } @Override - public MessageBuf newOutboundBuffer( - ChannelHandlerContext ctx) throws Exception { + public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception { return encoder.newOutboundBuffer(ctx); } @Override - public void flush( - ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { encoder.flush(ctx, promise); } @@ -81,9 +101,12 @@ public abstract class ByteToMessageCodec } public boolean isEncodable(Object msg) throws Exception { - return encoder.isEncodable(msg); + return ChannelHandlerUtil.acceptMessage(encodableMessageTypes, msg); } protected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, ByteBuf out) throws Exception; protected abstract INBOUND_OUT decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception; -} + protected INBOUND_OUT decodeLast(ChannelHandlerContext ctx, ByteBuf in) throws Exception { + return decode(ctx, in); + } +} \ No newline at end of file