Search code examples
c++avro

Size of encoded avro message without encoding it


Is there a way to get the size of the encoded avro message without actually encoding it?

I'm using Avro 1.8.1 for C++.

I'm used to google protocol buffers where you can call ByteSize() on a protobuf to get the encoded size, so it's something similar i'm looking for.

Since the message in essence is a raw struct I get that the size cannot be retrieved from the message itself, but perhaps there is a helper method that i'm not aware of?


Solution

  • There is no way around it unfortunately...

    Here is an example showing how the size can be calculated by encoding the object:

    MyAvroStruct obj;
    
    avro::EncoderPtr encoder = avro::binaryEncoder();
    std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream(1);
    encoder->init(*out);
    avro::encode(*encoder, obj);
    out->flush();
    uint32_t bufferSize = out->byteCount();