Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import io.netty.buffer.ByteBuf;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.handler.codec.ReplayingDecoder;
- public class MessageDecoder extends ReplayingDecoder<DecoderState> {
- private int length;
- public MessageDecoder()
- {
- super(DecoderState.READ_LENGTH);
- }
- @Override
- protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception{
- System.out.println(buf.readableBytes());
- switch(state()){
- case READ_LENGTH:
- length=buf.readInt();
- System.out.println("length is: "+length);
- checkpoint(DecoderState.READ_CONTENT);
- case READ_CONTENT:
- ByteBuf frame = buf.readBytes(length);
- checkpoint(DecoderState.READ_LENGTH);
- out.add(frame);
- break;
- default:
- throw new Error("Shouldn't reach here");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement