Add ByteBuffer copy for vector of bytes in Java (#5587)

This commit is contained in:
Derek Bailey
2019-10-28 09:30:31 -07:00
committed by Wouter van Oortmerssen
parent b4774d2354
commit 8d5e424c65
7 changed files with 221 additions and 47 deletions

View File

@@ -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.