MyMessageDecoder.java 904 B

1234567891011121314151617181920212223242526
  1. package cn.com.zyjblogs.chat.netty;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.channel.ChannelHandlerContext;
  4. import io.netty.handler.codec.ReplayingDecoder;
  5. import java.util.List;
  6. public class MyMessageDecoder extends ReplayingDecoder<MessageProtocol> {
  7. @Override
  8. protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  9. System.out.println("\nMyMessageDecoder方法被调用 解码");
  10. //需要将得到的二进制字节码 转成 MessageProtocol
  11. int length = in.readInt();
  12. byte[] content = new byte[length];
  13. in.readBytes(content);
  14. //封装成MessageProtocol对象,放入out中,传给下一个handler
  15. MessageProtocol messageProtocol = new MessageProtocol();
  16. messageProtocol.setLen(length);
  17. messageProtocol.setContent(content);
  18. out.add(messageProtocol);
  19. }
  20. }