diff options
author | Derek Bailey <dbaileychess@gmail.com> | 2019-10-28 09:30:31 -0700 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-10-28 09:30:31 -0700 |
commit | 8d5e424c6549b904504f17ddd14b76bb3cdbe8d0 (patch) | |
tree | 6ac0be89c336ad2666b1840a20a7fa5ed8a69200 /java/com | |
parent | b4774d2354425b29b233170cdef23c97c03d4aa8 (diff) | |
download | flatbuffers-8d5e424c6549b904504f17ddd14b76bb3cdbe8d0.tar.gz flatbuffers-8d5e424c6549b904504f17ddd14b76bb3cdbe8d0.tar.bz2 flatbuffers-8d5e424c6549b904504f17ddd14b76bb3cdbe8d0.zip |
Add ByteBuffer copy for vector of bytes in Java (#5587)
Diffstat (limited to 'java/com')
-rw-r--r-- | java/com/google/flatbuffers/FlatBufferBuilder.java | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index 7341b015..e5e3967a 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -572,6 +572,38 @@ public class FlatBufferBuilder { return endVector(); } + /** + * Create a byte array in the buffer. + * + * @param arr a source array with data. + * @param offset the offset in the source array to start copying from. + * @param length the number of bytes to copy from the source array. + * @return The offset in the buffer where the encoded array starts. + */ + public int createByteVector(byte[] arr, int offset, int length) { + startVector(1, length, 1); + bb.position(space -= length); + bb.put(arr, offset, length); + return endVector(); + } + + /** + * Create a byte array in the buffer. + * + * The source {@link ByteBuffer} position is advanced by {@link ByteBuffer#remaining()} places + * after this call. + * + * @param byteBuffer A source {@link ByteBuffer} with data. + * @return The offset in the buffer where the encoded array starts. + */ + public int createByteVector(ByteBuffer byteBuffer) { + int length = byteBuffer.remaining(); + startVector(1, length, 1); + bb.position(space -= length); + bb.put(byteBuffer); + return endVector(); + } + /// @cond FLATBUFFERS_INTERNAL /** * Should not be accessing the final buffer before it is finished. |