mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-04 20:48:59 +00:00
This commit is contained in:
@@ -34,6 +34,11 @@ public class ArrayReadWriteBuf implements ReadWriteBuf {
|
||||
this.writePos = startPos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
this.writePos = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(int index) {
|
||||
return buffer[index] != 0;
|
||||
|
||||
@@ -12,6 +12,11 @@ public class ByteBufferReadWriteBuf implements ReadWriteBuf {
|
||||
this.buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(int index) {
|
||||
return get(index) != 0;
|
||||
|
||||
@@ -151,6 +151,17 @@ public class FlexBuffersBuilder {
|
||||
this(bb, BUILDER_FLAG_SHARE_KEYS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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" } }
|
||||
|
||||
Reference in New Issue
Block a user