// Path: zing-common/src/main/java/org/zxc/zing/common/serialize/Serializer.java
// public class Serializer {
// 
//     private static final Logger log = LoggerFactory.getLogger(Serializer.class);
// 
// 
//     public static byte[] serialize(Object obj) throws IOException {
//         ByteArrayOutputStream bos = new ByteArrayOutputStream();
//         Hessian2Output out = new Hessian2Output(bos);
//         out.writeObject(obj);
//         out.close();
//         return bos.toByteArray();
//     }
// 
//     public static Object deserialize(byte[] data) throws IOException {
//         ByteArrayInputStream bis = new ByteArrayInputStream(data);
//         Hessian2Input in = new Hessian2Input(bis);
//         return in.readObject();
//     }
// }


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zxc.zing.common.serialize.Serializer;
import java.util.List;
package org.zxc.zing.common.handler;



/**
 * Created by xuanchen.zhao on 15-12-15.
 */
public class NettyDecoder extends ByteToMessageDecoder{

    private static final Logger log = LoggerFactory.getLogger(NettyDecoder.class);

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        log.info("try decode:"+in.toString());

        if (in.readableBytes() < 4) {
            log.info("no enough readable bytes");
            return;
        }

        int dataLength = in.readInt();
        if (dataLength < 0) {
            ctx.close();
        }

        log.info("try decode data length:"+dataLength);

        if (in.readableBytes() < dataLength) {
            in.resetReaderIndex();
        }

        log.info("try decode doDecode");

        byte[] data = new byte[dataLength];
        in.readBytes(data);

// Path: zing-common/src/main/java/org/zxc/zing/common/serialize/Serializer.java
// public class Serializer {
// 
//     private static final Logger log = LoggerFactory.getLogger(Serializer.class);
// 
// 
//     public static byte[] serialize(Object obj) throws IOException {
//         ByteArrayOutputStream bos = new ByteArrayOutputStream();
//         Hessian2Output out = new Hessian2Output(bos);
//         out.writeObject(obj);
//         out.close();
//         return bos.toByteArray();
//     }
// 
//     public static Object deserialize(byte[] data) throws IOException {
//         ByteArrayInputStream bis = new ByteArrayInputStream(data);
//         Hessian2Input in = new Hessian2Input(bis);
//         return in.readObject();
//     }
// }


// Path: zing-common/src/main/java/org/zxc/zing/common/handler/NettyDecoder.java
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zxc.zing.common.serialize.Serializer;
import java.util.List;

package org.zxc.zing.common.handler;



/**
 * Created by xuanchen.zhao on 15-12-15.
 */
public class NettyDecoder extends ByteToMessageDecoder{

    private static final Logger log = LoggerFactory.getLogger(NettyDecoder.class);

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        log.info("try decode:"+in.toString());

        if (in.readableBytes() < 4) {
            log.info("no enough readable bytes");
            return;
        }

        int dataLength = in.readInt();
        if (dataLength < 0) {
            ctx.close();
        }

        log.info("try decode data length:"+dataLength);

        if (in.readableBytes() < dataLength) {
            in.resetReaderIndex();
        }

        log.info("try decode doDecode");

        byte[] data = new byte[dataLength];
        in.readBytes(data);

        Object deserialized = Serializer.deserialize(data);