From 8945fb07ba33612d55c6a612bc72b1bc5abba8d9 Mon Sep 17 00:00:00 2001 From: Joris Gillis Date: Fri, 17 Apr 2020 10:43:10 +0200 Subject: [PATCH] Creating an reading a Person --- pom.xml | 11 ++- src/main/fbs/person.fbs | 5 +- .../flatbuffers/FlatbuffersApplication.java | 2 +- .../java/com/example/flatbuffers/Util.java | 10 ++ .../flatbuffers/WorkingOnFlatBuffers.java | 97 +++++++++++++++++++ 5 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/example/flatbuffers/Util.java create mode 100644 src/main/java/com/example/flatbuffers/WorkingOnFlatBuffers.java diff --git a/pom.xml b/pom.xml index 120236c..506a872 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,15 @@ flatbuffers-java 1.12.0 + + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + @@ -54,9 +63,7 @@ /usr/local/bin/flatc ${fbs.sources} - --grpc --java - --gen-mutable -o ${fbs.generated.sources} person.fbs diff --git a/src/main/fbs/person.fbs b/src/main/fbs/person.fbs index f1c8d0c..6fbf415 100644 --- a/src/main/fbs/person.fbs +++ b/src/main/fbs/person.fbs @@ -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; } diff --git a/src/main/java/com/example/flatbuffers/FlatbuffersApplication.java b/src/main/java/com/example/flatbuffers/FlatbuffersApplication.java index 9e826d3..6085eb3 100644 --- a/src/main/java/com/example/flatbuffers/FlatbuffersApplication.java +++ b/src/main/java/com/example/flatbuffers/FlatbuffersApplication.java @@ -3,6 +3,6 @@ package com.example.flatbuffers; public class FlatbuffersApplication { public static void main(String[] args) { - + new WorkingOnFlatBuffers().test(); } } diff --git a/src/main/java/com/example/flatbuffers/Util.java b/src/main/java/com/example/flatbuffers/Util.java new file mode 100644 index 0000000..66f6b45 --- /dev/null +++ b/src/main/java/com/example/flatbuffers/Util.java @@ -0,0 +1,10 @@ +package com.example.flatbuffers; + +import java.util.concurrent.ThreadLocalRandom; + +public class Util { + + public static T pickRandom(T[] options) { + return options[ThreadLocalRandom.current().nextInt(0, options.length)]; + } +} diff --git a/src/main/java/com/example/flatbuffers/WorkingOnFlatBuffers.java b/src/main/java/com/example/flatbuffers/WorkingOnFlatBuffers.java new file mode 100644 index 0000000..320c938 --- /dev/null +++ b/src/main/java/com/example/flatbuffers/WorkingOnFlatBuffers.java @@ -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); + } +} -- 2.45.2