Advertisement
Guest User

Untitled

a guest
May 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import java.util.List;
  2.  
  3. import io.netty.buffer.ByteBuf;
  4. import io.netty.channel.ChannelHandlerContext;
  5. import io.netty.handler.codec.ReplayingDecoder;
  6.  
  7. public class MessageDecoder extends ReplayingDecoder<DecoderState> {
  8.  
  9. private int length;
  10.  
  11. public MessageDecoder()
  12. {
  13. super(DecoderState.READ_LENGTH);
  14. }
  15.  
  16. @Override
  17. protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception{
  18. System.out.println(buf.readableBytes());
  19. switch(state()){
  20. case READ_LENGTH:
  21. length=buf.readInt();
  22. System.out.println("length is: "+length);
  23. checkpoint(DecoderState.READ_CONTENT);
  24. case READ_CONTENT:
  25. ByteBuf frame = buf.readBytes(length);
  26. checkpoint(DecoderState.READ_LENGTH);
  27. out.add(frame);
  28. break;
  29. default:
  30. throw new Error("Shouldn't reach here");
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement