diff options
author | Markus Junginger <greenrobot@users.noreply.github.com> | 2020-05-07 21:26:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-07 12:26:55 -0700 |
commit | c2da8d5d8548fa3ca43e9216e971750b1ad95760 (patch) | |
tree | 3d496209defefb1e9e46bb9df62b550456bc0dfd /java/com/google | |
parent | e84cbff673b59b37ab52d752679726d204861b33 (diff) | |
download | flatbuffers-c2da8d5d8548fa3ca43e9216e971750b1ad95760.tar.gz flatbuffers-c2da8d5d8548fa3ca43e9216e971750b1ad95760.tar.bz2 flatbuffers-c2da8d5d8548fa3ca43e9216e971750b1ad95760.zip |
[Java][FlexBuffers] Make FlexBuffersBuilder reusable by adding clear() (#5889) (#5890)
Diffstat (limited to 'java/com/google')
4 files changed, 28 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. |