Laurent
Laurent

Reputation: 14401

Missing helpers to create vector of structs in Java

Let's assume the following FlatBuffer schema as an example:

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;

The problem I have is with the creation of a vector that contains Ipv6 structs. The Java class Ipv6List generated by Flatbuffer 1.11.0 does not include the usual create helper. Reading the documentation it seems to be a design choice to improve performance by preventing the creation of a temp object.

Looking at other methods, there is a Ipv6List#startEntriesVector static function but no associated addX and endX function. Here is what I am trying to do:

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();

Any idea how to create a vector of Ipv6 struct and to associate it with the list?

Upvotes: 1

Views: 351

Answers (1)

Aardappel
Aardappel

Reputation: 6074

Structs always need to be created inline, so the order of operations should be:

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

Upvotes: 1

Related Questions