Search in sources :

Example 11 with ByteBufCodedOutputStream

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;
}
Also used : IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream)

Aggregations

ByteBufCodedOutputStream (org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream)11 ByteBuf (io.netty.buffer.ByteBuf)10 IOException (java.io.IOException)7 PulsarApi (org.apache.pulsar.common.api.proto.PulsarApi)2 BaseCommand (org.apache.pulsar.common.api.proto.PulsarApi.BaseCommand)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteBufPair (org.apache.pulsar.common.api.ByteBufPair)1 MessageIdData (org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData)1 MessageMetadata (org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata)1 ByteBufCodedInputStream (org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream)1 Test (org.testng.annotations.Test)1