/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.handler.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.MessageBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundByteHandler;
import io.netty.channel.ChannelOutboundMessageHandler;
import io.netty.channel.ChannelPromise;

public abstract class ByteToMessageCodec<INBOUND_OUT, OUTBOUND_IN>
        extends ChannelHandlerAdapter
        implements ChannelInboundByteHandler, ChannelOutboundMessageHandler<OUTBOUND_IN> {

    private final MessageToByteEncoder<OUTBOUND_IN> encoder;
    private final ByteToMessageDecoder<INBOUND_OUT> decoder;

    protected ByteToMessageCodec(Class<?>... encodableMessageTypes) {
        encoder = new MessageToByteEncoder<OUTBOUND_IN>(encodableMessageTypes) {
            @Override
            protected void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, ByteBuf out) throws Exception {
                ByteToMessageCodec.this.encode(ctx, msg, out);
            }
        };

        decoder = new ByteToMessageDecoder<INBOUND_OUT>() {
            @Override
            public INBOUND_OUT decode(
                    ChannelHandlerContext ctx, ByteBuf in) throws Exception {
                return ByteToMessageCodec.this.decode(ctx, in);
            }
        };
    }

    @Override
    public ByteBuf newInboundBuffer(
            ChannelHandlerContext ctx) throws Exception {
        return decoder.newInboundBuffer(ctx);
    }

    @Override
    public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
        decoder.inboundBufferUpdated(ctx);
    }

    @Override
    public MessageBuf<OUTBOUND_IN> newOutboundBuffer(
            ChannelHandlerContext ctx) throws Exception {
        return encoder.newOutboundBuffer(ctx);
    }

    @Override
    public void flush(
            ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
        encoder.flush(ctx, promise);
    }

    @Override
    public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
        decoder.freeInboundBuffer(ctx);
    }

    @Override
    public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
        encoder.freeOutboundBuffer(ctx);
    }

    public boolean isEncodable(Object msg) throws Exception {
        return encoder.isEncodable(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;
}

@@ -822,16 +822,60 @@
 ontext;%0A
+import io.netty.channel.ChannelHandlerUtil;%0A
 import i
@@ -1210,16 +1210,68 @@
 _IN%3E %7B%0A%0A
+    private final Class%3C?%3E%5B%5D encodableMessageTypes;%0A
     priv
@@ -1467,80 +1467,291 @@
     
-encoder = new MessageToByteEncoder%3COUTBOUND_IN%3E(encodableMessageTypes) %7B
+this.encodableMessageTypes = encodableMessageTypes;%0A        encoder = new MessageToByteEncoder%3COUTBOUND_IN%3E() %7B%0A            @Override%0A            public boolean isEncodable(Object msg) throws Exception %7B%0A                return ByteToMessageCodec.this.isEncodable(msg);%0A            %7D%0A
 %0A   
@@ -2092,29 +2092,213 @@
 ode(
-%0A                    
+ChannelHandlerContext ctx, ByteBuf in) throws Exception %7B%0A                return ByteToMessageCodec.this.decode(ctx, in);%0A%0A            %7D%0A%0A            @Override%0A            protected INBOUND_OUT decodeLast(
 Chan
@@ -2396,32 +2396,36 @@
 odec.this.decode
+Last
 (ctx, in);%0A     
@@ -2433,25 +2433,351 @@
       %7D%0A
-        %7D
+%0A            @Override%0A            public void discardInboundReadBytes(ChannelHandlerContext ctx) throws Exception %7B%0A                ByteToMessageCodec.this.discardInboundReadBytes(ctx);%0A            %7D%0A        %7D;%0A    %7D%0A%0A    @Override%0A    public void beforeAdd(ChannelHandlerContext ctx) throws Exception %7B%0A        decoder.beforeAdd(ctx)
 ;%0A    %7D%0A
@@ -2823,37 +2823,24 @@
 boundBuffer(
-%0A            
 ChannelHandl
@@ -3140,29 +3140,16 @@
 dBuffer(
-%0A            
 ChannelH
@@ -3276,29 +3276,16 @@
 d flush(
-%0A            
 ChannelH
@@ -3755,28 +3755,64 @@
 urn 
-encoder.isEncodable(
+ChannelHandlerUtil.acceptMessage(encodableMessageTypes, 
 msg)
@@ -4029,10 +4029,142 @@
 eption;%0A
+    protected INBOUND_OUT decodeLast(ChannelHandlerContext ctx, ByteBuf in) throws Exception %7B%0A        return decode(ctx, in);%0A    %7D%0A
 %7D
-%0A

Add missing codec operations in ByteToMessageCodec