Search code examples
javaprotocol-buffersprotostuff

How to tell protostuff to pack property to fixed32 and not int32


I'm trying to serialize the following Java object to protobuf using protostuff:

public class HeaderTest
{

  private int version;
  private UUID messageId;

  public HeaderTest() {} // required by jackson

  public HeaderTest(UUID messageId, int version)
  {

    this.messageId = messageId;
    this.version = version;

  }

  public int getVersion() {
    return version;
  }

  public void setVersion(int version) {
    this.version = version;
  }

  public UUID getMessageId() {
    return messageId;
  }

  public void setMessageId(UUID messageId) {
    this.messageId = messageId;
  }
}

With the following code:

Schema<HeaderTest> headerTestSchema = RuntimeSchema.getSchema(HeaderTest.class);
byte[] headerTestBuff = ProtostuffIOUtil.toByteArray(headerTestInstance, headerTestSchema, LinkedBuffer.allocate());

I would like to get fixed size buffer but protostuff serialize the version integer as varint type ( the amount of bytes use to represent the integer changes according to the integer size )

How can I tell protostuff to serialize specific property as fixed32 with fix amount of bytes

Thanks


Solution

  • RuntimeSchema does not allow you to select fixed32 as a type for integer field. It can use only int32/int64.

    The only thing that you can do - you can estimate maximum size of the buffer. Each int32 takes at most 5 bytes.