[Java] Add support for shared strings on FlatBufferBuilder. (#6012)

Added a method FlatBufferBuilder::createSharedString that
enable string sharing for building messages on java.

The shared pool will only contains strings inserted by
this methods.
This commit is contained in:
Paulo Pinheiro
2020-07-02 21:40:56 +02:00
committed by GitHub
parent ab6af18d9f
commit b69fc8cc95
3 changed files with 80 additions and 15 deletions

View File

@@ -23,7 +23,9 @@ import java.io.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -101,6 +103,8 @@ class JavaTest {
TestVectorOfBytes();
TestSharedStringPool();
System.out.println("FlatBuffers test: completed successfully");
}
@@ -1216,6 +1220,15 @@ class JavaTest {
TestEq(monsterObject8.inventoryLength(), 2048);
}
static void TestSharedStringPool() {
FlatBufferBuilder fb = new FlatBufferBuilder(1);
String testString = "My string";
int offset = fb.createSharedString(testString);
for (int i=0; i< 10; i++) {
TestEq(offset, fb.createSharedString(testString));
}
}
static <T> void TestEq(T a, T b) {
if (!a.equals(b)) {
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());