16 Mayıs 2021 Pazar

Netty ReplayingDecoder Sınıfı - ByteBuf Nesnesini Java Nesnesine Çevirir

Giriş
Açıklaması şöyle.
It uses an implementation of ByteBuf which throws an exception when there is not enough data in the buffer for the reading operation.

When the exception is caught the buffer is rewound to the beginning and the decoder waits for a new portion of data. Decoding stops when the out list is not empty after decode execution.
ByteToMessageDecoder sınıfında kalıtır.

Örnek
Şöyle yaparız
public class RequestDecoder extends ReplayingDecoder<RequestData> {

  private final Charset charset = Charset.forName("UTF-8");

  @Override
  protected void decode(ChannelHandlerContext ctx, 
    ByteBuf in, List<Object> out) throws Exception {
 
    RequestData data = new RequestData();
    data.setIntValue(in.readInt());
    int strLen = in.readInt();
    data.setStringValue(
      in.readCharSequence(strLen, charset).toString());
    out.add(data);
  }
}

Hiç yorum yok:

Yorum Gönder