Skip to content

Commit

Permalink
update with 1.10.0 google java classes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmoten committed Dec 2, 2018
1 parent 95d1815 commit 94a7a91
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ public interface ByteBufferFactory {
* @return Returns the new `ByteBuffer` that was allocated.
*/
ByteBuffer newByteBuffer(int capacity);

/**
* Release a ByteBuffer. Current {@link FlatBufferBuilder}
* released any reference to it, so it is safe to dispose the buffer
* or return it to a pool.
* It is not guaranteed that the buffer has been created
* with {@link #newByteBuffer(int) }.
*
* @param bb the buffer to release
*/
default void releaseByteBuffer(ByteBuffer bb) {
}
}

/**
Expand Down Expand Up @@ -241,7 +253,11 @@ public void prep(int size, int additional_bytes) {
// Reallocate the buffer if needed.
while (space < align_size + size + additional_bytes) {
int old_buf_size = bb.capacity();
bb = growByteBuffer(bb, bb_factory);
ByteBuffer old = bb;
bb = growByteBuffer(old, bb_factory);
if (old != bb) {
bb_factory.releaseByteBuffer(old);
}
space += bb.capacity() - old_buf_size;
}
pad(align_size);
Expand Down
14 changes: 14 additions & 0 deletions flatbuffers-java/src/main/java/com/google/flatbuffers/Struct.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public class Struct {
protected int bb_pos;
/** The underlying ByteBuffer to hold the data of the Struct. */
protected ByteBuffer bb;

/**
* Resets internal state with a null {@code ByteBuffer} and a zero position.
*
* This method exists primarily to allow recycling Struct instances without risking memory leaks
* due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
* again to a {@code ByteBuffer}.
*
* @param struct the instance to reset to initial state
*/
public void __reset() {
bb = null;
bb_pos = 0;
}
}

/// @endcond
12 changes: 12 additions & 0 deletions flatbuffers-java/src/main/java/com/google/flatbuffers/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ protected static int compareStrings(int offset_1, byte[] key, ByteBuffer bb) {
}
return len_1 - len_2;
}

/**
* Resets the internal state with a null {@code ByteBuffer} and a zero position.
*
* This method exists primarily to allow recycling Table instances without risking memory leaks
* due to {@code ByteBuffer} references. The instance will be unusable until it is assigned
* again to a {@code ByteBuffer}.
*/
public void __reset() {
bb = null;
bb_pos = 0;
}
}

/// @endcond

0 comments on commit 94a7a91

Please sign in to comment.