use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.
the class RawMessageImpl method serialize.
@Override
public ByteBuf serialize() {
ByteBuf headersAndPayload = this.headersAndPayload.slice();
// Format: [IdSize][Id][PayloadAndMetadataSize][PayloadAndMetadata]
int idSize = id.getSerializedSize();
int headerSize = 4 + /* IdSize */
idSize + 4;
int totalSize = headerSize + headersAndPayload.readableBytes();
ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(totalSize);
buf.writeInt(idSize);
try {
ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(buf);
id.writeTo(outStream);
outStream.recycle();
} catch (IOException e) {
// This is in-memory serialization, should not fail
log.error("IO exception serializing to ByteBuf (this shouldn't happen as operation is in-memory)", e);
throw new RuntimeException(e);
}
buf.writeInt(headersAndPayload.readableBytes());
buf.writeBytes(headersAndPayload);
return buf;
}
Aggregations