use of org.apache.ratis.io.WriteOption in project incubator-ratis by apache.
the class NettyDataStreamUtils method decodeDataStreamRequestHeader.
static DataStreamRequestHeader decodeDataStreamRequestHeader(ByteBuf buf) {
if (DataStreamPacketHeader.getSizeOfHeaderBodyLen() > buf.readableBytes()) {
return null;
}
long headerBodyBufLen = buf.readLong();
if (headerBodyBufLen > buf.readableBytes()) {
buf.resetReaderIndex();
return null;
}
int headerBufLen = buf.readInt();
if (headerBufLen > buf.readableBytes()) {
buf.resetReaderIndex();
return null;
}
try {
ByteBuf headerBuf = buf.slice(buf.readerIndex(), headerBufLen);
DataStreamRequestHeaderProto header = DataStreamRequestHeaderProto.parseFrom(headerBuf.nioBuffer());
final DataStreamPacketHeaderProto h = header.getPacketHeader();
if (h.getDataLength() + headerBufLen <= buf.readableBytes()) {
buf.readerIndex(buf.readerIndex() + headerBufLen);
WriteOption[] options = new WriteOption[h.getOptionsCount()];
for (int i = 0; i < options.length; i++) {
options[i] = StandardWriteOption.values()[h.getOptions(i).ordinal()];
}
return new DataStreamRequestHeader(ClientId.valueOf(h.getClientId()), h.getType(), h.getStreamId(), h.getStreamOffset(), h.getDataLength(), options);
} else {
buf.resetReaderIndex();
return null;
}
} catch (InvalidProtocolBufferException e) {
LOG.error("Fail to decode request header:", e);
buf.resetReaderIndex();
return null;
}
}
Aggregations