summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Junginger <greenrobot@users.noreply.github.com>2020-05-07 21:26:55 +0200
committerGitHub <noreply@github.com>2020-05-07 12:26:55 -0700
commitc2da8d5d8548fa3ca43e9216e971750b1ad95760 (patch)
tree3d496209defefb1e9e46bb9df62b550456bc0dfd
parente84cbff673b59b37ab52d752679726d204861b33 (diff)
downloadflatbuffers-c2da8d5d8548fa3ca43e9216e971750b1ad95760.tar.gz
flatbuffers-c2da8d5d8548fa3ca43e9216e971750b1ad95760.tar.bz2
flatbuffers-c2da8d5d8548fa3ca43e9216e971750b1ad95760.zip
[Java][FlexBuffers] Make FlexBuffersBuilder reusable by adding clear() (#5889) (#5890)
-rw-r--r--java/com/google/flatbuffers/ArrayReadWriteBuf.java5
-rw-r--r--java/com/google/flatbuffers/ByteBufferReadWriteBuf.java5
-rw-r--r--java/com/google/flatbuffers/FlexBuffersBuilder.java11
-rw-r--r--java/com/google/flatbuffers/ReadWriteBuf.java7
-rw-r--r--tests/JavaTest.java10
5 files changed, 38 insertions, 0 deletions
diff --git a/java/com/google/flatbuffers/ArrayReadWriteBuf.java b/java/com/google/flatbuffers/ArrayReadWriteBuf.java
index 00517747..38412e55 100644
--- a/java/com/google/flatbuffers/ArrayReadWriteBuf.java
+++ b/java/com/google/flatbuffers/ArrayReadWriteBuf.java
@@ -35,6 +35,11 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
}
@Override
+ public void clear() {
+ this.writePos = 0;
+ }
+
+ @Override
public boolean getBoolean(int index) {
return buffer[index] != 0;
}
diff --git a/java/com/google/flatbuffers/ByteBufferReadWriteBuf.java b/java/com/google/flatbuffers/ByteBufferReadWriteBuf.java
index 79ad7bbb..aaf72fe8 100644
--- a/java/com/google/flatbuffers/ByteBufferReadWriteBuf.java
+++ b/java/com/google/flatbuffers/ByteBufferReadWriteBuf.java
@@ -13,6 +13,11 @@ public class ByteBufferReadWriteBuf implements ReadWriteBuf {
}
@Override
+ public void clear() {
+ buffer.clear();
+ }
+
+ @Override
public boolean getBoolean(int index) {
return get(index) != 0;
}
diff --git a/java/com/google/flatbuffers/FlexBuffersBuilder.java b/java/com/google/flatbuffers/FlexBuffersBuilder.java
index cb44492a..dc0df966 100644
--- a/java/com/google/flatbuffers/FlexBuffersBuilder.java
+++ b/java/com/google/flatbuffers/FlexBuffersBuilder.java
@@ -152,6 +152,17 @@ public class FlexBuffersBuilder {
}
/**
+ * Reset the FlexBuffersBuilder by purging all data that it holds.
+ */
+ public void clear(){
+ bb.clear();
+ stack.clear();
+ keyPool.clear();
+ stringPool.clear();
+ finished = false;
+ }
+
+ /**
* Return `ByteBuffer` containing FlexBuffer message. {@code #finish()} must be called before calling this
* function otherwise an assert will trigger.
*
diff --git a/java/com/google/flatbuffers/ReadWriteBuf.java b/java/com/google/flatbuffers/ReadWriteBuf.java
index df672fbc..cae186e2 100644
--- a/java/com/google/flatbuffers/ReadWriteBuf.java
+++ b/java/com/google/flatbuffers/ReadWriteBuf.java
@@ -5,6 +5,13 @@ package com.google.flatbuffers;
* FlexBuffers message.
*/
interface ReadWriteBuf extends ReadBuf {
+
+ /**
+ * Clears (resets) the buffer so that it can be reused. Write position will be set to the
+ * start.
+ */
+ void clear();
+
/**
* Put a boolean into the buffer at {@code writePosition()} . Booleans as stored as single
* byte. Write position will be incremented.
diff --git a/tests/JavaTest.java b/tests/JavaTest.java
index afa1970a..a0dead96 100644
--- a/tests/JavaTest.java
+++ b/tests/JavaTest.java
@@ -591,7 +591,17 @@ class JavaTest {
public static void testFlexBuffersTest() {
FlexBuffersBuilder builder = new FlexBuffersBuilder(ByteBuffer.allocate(512),
FlexBuffersBuilder.BUILDER_FLAG_SHARE_KEYS_AND_STRINGS);
+ testFlexBuffersTest(builder);
+ int bufferLimit1 = ((ArrayReadWriteBuf) builder.getBuffer()).limit();
+
+ // Repeat after clearing the builder to ensure the builder is reusable
+ builder.clear();
+ testFlexBuffersTest(builder);
+ int bufferLimit2 = ((ArrayReadWriteBuf) builder.getBuffer()).limit();
+ TestEq(bufferLimit1, bufferLimit2);
+ }
+ public static void testFlexBuffersTest(FlexBuffersBuilder builder) {
// Write the equivalent of:
// { vec: [ -100, "Fred", 4.0, false ], bar: [ 1, 2, 3 ], bar3: [ 1, 2, 3 ],
// foo: 100, bool: true, mymap: { foo: "Fred" } }