我正在尝试使用 protostuff 将以下 Java 对象序列化为 protobuf:
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;
}
}
使用以下代码:
Schema<HeaderTest> headerTestSchema = RuntimeSchema.getSchema(HeaderTest.class);
byte[] headerTestBuff = ProtostuffIOUtil.toByteArray(headerTestInstance, headerTestSchema, LinkedBuffer.allocate());
我想获得固定大小的缓冲区,但 protostuff 将版本整数序列化为 varint 类型(用于表示整数的字节数根据整数大小而变化)
如何告诉 protostuff 将特定属性序列化为固定字节数的固定32
谢谢
最佳答案
RuntimeSchema
不允许您选择 fixed32
作为整数字段的类型。它只能使用 int32
/int64
。
你唯一能做的就是——你可以估计缓冲区的最大大小。每个int32
最多占用5个字节。
关于java - 如何告诉protostuff将属性打包到fixed32而不是int32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43070148/