"Fossies" - the Fresh Open Source Software Archive

Member "flatbuffers-23.1.21/docs/source/JavaUsage.md" (21 Jan 2023, 4783 Bytes) of package /linux/misc/flatbuffers-23.1.21.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format (assuming markdown format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.

Use in Java {#flatbuffers_guide_use_java}

Before you get started

Before diving into the FlatBuffers usage in Java, it should be noted that the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to general FlatBuffers usage in all of the supported languages (including Java). This page is designed to cover the nuances of FlatBuffers usage, specific to Java.

You should also have read the [Building](@ref flatbuffers_guide_building) documentation to build flatc and should be familiar with [Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and [Writing a schema](@ref flatbuffers_guide_writing_schema).

FlatBuffers Java code location

The code for the FlatBuffers Java library can be found at flatbuffers/java/com/google/flatbuffers. You can browse the library on the [FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/ java/com/google/flatbuffers).

Testing the FlatBuffers Java libraries

The code to test the libraries can be found at flatbuffers/tests.

The test code for Java is located in [JavaTest.java](https://github.com/google /flatbuffers/blob/master/tests/JavaTest.java).

To run the tests, use either [JavaTest.sh](https://github.com/google/ flatbuffers/blob/master/tests/JavaTest.sh) or [JavaTest.bat](https://github.com/ google/flatbuffers/blob/master/tests/JavaTest.bat), depending on your operating system.

Note: These scripts require that Java is installed.

Using the FlatBuffers Java library

Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth example of how to use FlatBuffers in Java.

FlatBuffers supports reading and writing binary FlatBuffers in Java.

To use FlatBuffers in your own code, first generate Java classes from your schema with the --java option to flatc. Then you can include both FlatBuffers and the generated code to read or write a FlatBuffer.

For example, here is how you would read a FlatBuffer binary file in Java: First, import the library and generated code. Then, you read a FlatBuffer binary file into a byte[]. You then turn the byte[] into a ByteBuffer, which you pass to the getRootAsMyRootType function:

    import MyGame.Example.*;
    import com.google.flatbuffers.FlatBufferBuilder;

    // This snippet ignores exceptions for brevity.
    File file = new File("monsterdata_test.mon");
    RandomAccessFile f = new RandomAccessFile(file, "r");
    byte[] data = new byte[(int)f.length()];
    f.readFully(data);
    f.close();

    ByteBuffer bb = ByteBuffer.wrap(data);
    Monster monster = Monster.getRootAsMonster(bb);

Now you can access the data from the Monster monster:

    short hp = monster.hp();
    Vec3 pos = monster.pos();

Storing dictionaries in a FlatBuffer

FlatBuffers doesn't support dictionaries natively, but there is support to emulate their behavior with vectors and binary search, which means you can have fast lookups directly from a FlatBuffer without having to unpack your data into a Dictionary or similar.

To use it:

Text parsing

There currently is no support for parsing text (Schema's and JSON) directly from Java, though you could use the C++ parser through native call interfaces available to each language. Please see the C++ documentation for more on text parsing.