/*
 * 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 =
            new MessageToByteEncoder<OUTBOUND_IN>() {
        @Override
        public void encode(
                ChannelHandlerContext ctx,
                OUTBOUND_IN msg, ByteBuf out) throws Exception {
            ByteToMessageCodec.this.encode(ctx, msg, out);
        }
    };

    private final ByteToMessageDecoder<INBOUND_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);
    }

    protected abstract void encode(
            ChannelHandlerContext ctx,
            OUTBOUND_IN msg, ByteBuf out) throws Exception;

    protected abstract INBOUND_OUT decode(
            ChannelHandlerContext ctx, ByteBuf in) throws Exception;
}

@@ -1229,127 +1229,279 @@
 oder
- =%0A            new MessageToByteEncoder%3COUTBOUND_IN%3E() %7B%0A        @Override%0A        public void encode(%0A                
+;%0A    private final ByteToMessageDecoder%3CINBOUND_OUT%3E decoder;%0A%0A    protected ByteToMessageCodec(Class%3C?%3E... encodableMessageTypes) %7B%0A        encoder = new MessageToByteEncoder%3COUTBOUND_IN%3E(encodableMessageTypes) %7B%0A            @Override%0A            protected void encode(
 Chan
@@ -1522,32 +1522,16 @@
 ext ctx,
-%0A               
  OUTBOUN
@@ -1584,16 +1584,20 @@
         
+   
+ 
 ByteToMe
@@ -1635,34 +1635,42 @@
 , out);%0A        
-%7D%0A
+    %7D%0A    
     %7D;%0A%0A    priv
@@ -1669,77 +1669,21 @@
     
-private final ByteToMessageDecoder%3CINBOUND_OUT%3E decoder =%0A           
+    decoder =
  new
@@ -1721,32 +1721,36 @@
 UT%3E() %7B%0A        
+ 
+   
 @Override%0A      
@@ -1735,32 +1735,36 @@
       @Override%0A
+    
         public I
@@ -1790,32 +1790,36 @@
                 
+   
+ 
 ChannelHandlerCo
@@ -1868,24 +1868,28 @@
             
+    
 return ByteT
@@ -1936,16 +1936,30 @@
     
+    
 %7D%0A    
+ 
+   %7D;%0A    
 %7D
-;
 %0A%0A  
@@ -2910,42 +2910,142 @@
 ted 
-abstract void encode(
+boolean isEncodable(Object msg) throws Exception %7B%0A        return encoder.isEncodable(msg);
 %0A    
- 
+%7D%0A%0A
     
-   
+protected abstract void encode(
 Chan
@@ -3066,28 +3066,16 @@
 ext ctx,
-%0A           
  OUTBOUN
@@ -3115,17 +3115,16 @@
 eption;%0A
-%0A
     prot
@@ -3157,29 +3157,16 @@
  decode(
-%0A            
 ChannelH

Add ByteToMessageCodec.isEncodable()