java - 如何告诉protostuff将属性打包到fixed32而不是int32

标签 java protocol-buffers protostuff

我正在尝试使用 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/

相关文章:

java - 如何解决 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

java - 哪个工具/插件可用于在尽可能短的时间内从远程位置复制文件?

java - 如何在 Java Applet 中正确使用 getDocumentBase() 和 getCodeBase()?

java - 为什么在结合使用泛型和循环引用时 protostuff 会损坏对象?

java - 如何使用Struts 2和Hibernate维护 session ?

protocol-buffers - Protocol Buffer - 唯一编号标签 - 澄清?

c++ - Protobuf 消息中的消息标识符

python - 属性错误 : 'google.protobuf.pyext._message.RepeatedCompositeCo' object has no attribute 'append'

java - 使用 Protostuff 默认 RuntimeSchema 时,在 java 类末尾添加字段总是安全的吗?