android:cannot read the flatbuffers data from file

284 Views Asked by At

I just started useing flatbuffers in my project. I write a simple demo application. The github project is here. This demo converts the pre-packaged json to flatbuffers and saves it the file, and prints the time cost by reading from JSON and reading from the flatbuffers file.

It seems successfully save the flatbuffer to file, I can see the file, plugin, in the application interval file dir and its size is not zero, which is about 280k if the original JSON is about 500k. But I failed to read the data from flatbuffer.

Reading the plugin file is successful, the ByteBuffer is non-zero. But when parsing it

    Plugins plugins = Plugins.getRootAsPlugins(byteBuffer);
    int length = plugins.pluginLength();

I find the length is zero. So there is some problems when parsing it. How to read it from the flatbuffers file?

1

There are 1 best solutions below

0
On

You do a FileOutputStream.write on the underlying ByteBuffer you got from FlatBuffers via FlatBufferBuilder.dataBuffer. This buffer stores the FlatBuffer data, but it does not necessarily start at 0, you need to write from ByteBuffer.position onwards.

See also https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html (select Java).