~jgillis/flatbuffers-read-writing

8945fb07ba33612d55c6a612bc72b1bc5abba8d9 — Joris Gillis 4 years ago 03cc0b3
Creating an reading a Person
M pom.xml => pom.xml +9 -2
@@ 26,6 26,15 @@
            <artifactId>flatbuffers-java</artifactId>
            <version>1.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
    </dependencies>

    <build>


@@ 54,9 63,7 @@
                    <executable>/usr/local/bin/flatc</executable>
                    <workingDirectory>${fbs.sources}</workingDirectory>
                    <arguments>
                        <argument>--grpc</argument>
                        <argument>--java</argument>
                        <argument>--gen-mutable</argument>
                        <argument>-o</argument>
                        <argument>${fbs.generated.sources}</argument>
                        <argument>person.fbs</argument>

M src/main/fbs/person.fbs => src/main/fbs/person.fbs +3 -2
@@ 3,12 3,13 @@ namespace com.example.flatbuffers.model;
table Person {
    firstName:string;
    lastName:string;
    age:int;
    age:short;
    address:Address;
}

table Address {
    street:string;
    streetNumber:int;
    streetNumber:short;
    city:string;
    postalCode:short;
}

M src/main/java/com/example/flatbuffers/FlatbuffersApplication.java => src/main/java/com/example/flatbuffers/FlatbuffersApplication.java +1 -1
@@ 3,6 3,6 @@ package com.example.flatbuffers;
public class FlatbuffersApplication {

    public static void main(String[] args) {

        new WorkingOnFlatBuffers().test();
    }
}

A src/main/java/com/example/flatbuffers/Util.java => src/main/java/com/example/flatbuffers/Util.java +10 -0
@@ 0,0 1,10 @@
package com.example.flatbuffers;

import java.util.concurrent.ThreadLocalRandom;

public class Util {

    public static <T> T pickRandom(T[] options) {
        return options[ThreadLocalRandom.current().nextInt(0, options.length)];
    }
}

A src/main/java/com/example/flatbuffers/WorkingOnFlatBuffers.java => src/main/java/com/example/flatbuffers/WorkingOnFlatBuffers.java +97 -0
@@ 0,0 1,97 @@
package com.example.flatbuffers;

import com.example.flatbuffers.model.Address;
import com.example.flatbuffers.model.Person;
import com.google.flatbuffers.FlatBufferBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.concurrent.ThreadLocalRandom;

import static com.example.flatbuffers.Util.pickRandom;

public class WorkingOnFlatBuffers {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkingOnFlatBuffers.class);

    private static final String[] FIRST_NAMES = new String[]{"John", "Jane", "Gerard", "Aubrey", "Amelia"};
    private static final String[] LAST_NAMES = new String[]{"Smith", "Parker", "Phillips", "Jones"};
    private static final String[] STREETS = new String[]{
            "Halloway", "Sunset Boulvard", "Wall Street", "Secret Passageway"};
    private static final String[] CITIES = new String[]{"Brussels", "Paris", "London", "Amsterdam"};
    public static final String PERSON_LINE_TEMPLATE = " * {}";
    public static final String ADDRESS_LINE_TEMPLATE = "   * {} {}";

    public void test() {
        // Writing data into buffer
        ByteBuffer data = createPerson();

        // Reading data from buffer
        Person person = Person.getRootAsPerson(data);

        // Printing out read data
        printPerson(person);
    }

    private void printPerson(Person person) {
        LOGGER.info("Person:");
        LOGGER.info(PERSON_LINE_TEMPLATE, person.firstName());
        LOGGER.info(PERSON_LINE_TEMPLATE, person.lastName());
        LOGGER.info(PERSON_LINE_TEMPLATE, person.age());
        LOGGER.info(" + Address:");
        LOGGER.info(ADDRESS_LINE_TEMPLATE, person.address().street(), person.address().streetNumber());
        LOGGER.info(ADDRESS_LINE_TEMPLATE, person.address().postalCode(), person.address().city());
    }

    /**
     * @return HeapByteBuffer
     */
    private ByteBuffer createPerson() {
        FlatBufferBuilder builder = new FlatBufferBuilder();

        int street = builder.createString(randomStreet());
        int city = builder.createString(randomCity());
        int address = Address.createAddress(builder, street, randomStreetNumber(), city, randomPostalCode());

        int firstName = builder.createString(randomFirstName());
        int lastName = builder.createString(randomLastName());
        Person.startPerson(builder);
        Person.addAge(builder, randomAge());
        Person.addFirstName(builder, firstName);
        Person.addLastName(builder, lastName);
        Person.addAddress(builder, address);
        int person = Person.endPerson(builder);

        builder.finish(person);
        return builder.dataBuffer();
    }

    private String randomFirstName() {
        return pickRandom(FIRST_NAMES);
    }

    private String randomLastName() {
        return pickRandom(LAST_NAMES);
    }

    private short randomAge() {
        return (short) ThreadLocalRandom.current().nextInt(0, 120);
    }

    private String randomStreet() {
        return pickRandom(STREETS);
    }

    private short randomStreetNumber() {
        return (short) ThreadLocalRandom.current().nextInt(1, 3000);
    }

    private String randomCity() {
        return pickRandom(CITIES);
    }

    private short randomPostalCode() {
        return (short) ThreadLocalRandom.current().nextInt(1000, 10000);
    }
}