【问题标题】:Missing helpers to create vector of structs in Java缺少帮助器在 Java 中创建结构向量
【发布时间】:2020-01-09 15:50:56
【问题描述】:

我们假设以下 FlatBuffer 架构为例:

struct Ipv6 {
    b0: byte;
    b1: byte;
    b2: byte;
    b3: byte;
    b4: byte;
    b5: byte;
    b6: byte;
    b7: byte;
    b8: byte;
    b9: byte;
    b10: byte;
    b11: byte;
    b12: byte;
    b13: byte;
    b14: byte;
    b15: byte;
}

table Ipv6List {
    entries: [Ipv6];
}

root_type Ipv6List;

我遇到的问题是创建一个包含 Ipv6 结构的向量。 Flatbuffer 1.11.0 生成的 Java 类 Ipv6List 不包括通常的 create 帮助器。阅读文档似乎是一种通过防止创建临时对象来提高性能的设计选择。

查看其他方法,有一个Ipv6List#startEntriesVector 静态函数,但没有关联的addXendX 函数。这是我想要做的:

FlatBufferBuilder builder = new FlatBufferBuilder();

final byte[] inetAddressBytes =
        Inet6Address.getByName("2a01:e35:2e7a:490:6193:c54c:f740:f907").getAddress();

int ipv6Offset = Ipv6.createIpv6(builder,
        inetAddressBytes[0], inetAddressBytes[1], inetAddressBytes[2], inetAddressBytes[3],
        inetAddressBytes[4], inetAddressBytes[5], inetAddressBytes[6], inetAddressBytes[7],
        inetAddressBytes[8], inetAddressBytes[9], inetAddressBytes[10], inetAddressBytes[11],
        inetAddressBytes[12], inetAddressBytes[13], inetAddressBytes[14], inetAddressBytes[15]
);

Ipv6List.startEntriesVector(builder, 1);

// how to add the IP to the vector ?
// how to end the association and get the vector offset ?
// int ipsVectorOffset = ?;

int ipListOffset = Ipv6List.createIpv6List(builder, ipsVectorOffset);
builder.finish(ipListOffset);
ByteBuffer byteBuffer = builder.dataBuffer();

知道如何创建 Ipv6 结构的向量并将其与列表关联吗?

【问题讨论】:

    标签: java flatbuffers


    【解决方案1】:

    结构体总是需要内联创建,所以操作顺序应该是:

    Ipv6List.startEntriesVector(builder, 1);
    Ipv6.createIpv6(builder,..);
    o = builder.endVector();
    Ipv6List.createIpv6List(builder, o);
    

    【讨论】:

    • 感谢您指出这个问题,但我的根本问题是甚至没有 Ipv6List.end... 方法 :( 看起来 Struct 需要成为 Table 的一部分才能生成 endX 方法。直接将其用作 Vector 不会生成方法。是要求还是错误?在前者中,在编译时引发错误会非常有帮助。
    • google.github.io/flatbuffers/flatbuffers_guide_tutorial.html 所示,您可以直接致电builder.endVector(),让我在答案中解决。
    猜你喜欢
    • 2014-02-14
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2017-01-13
    相关资源
    最近更新 更多