From e5a1a3129dba65de0bf5f49f34a574dd559d92eb Mon Sep 17 00:00:00 2001 From: pjulien Date: Wed, 6 May 2015 19:41:44 -0400 Subject: [PATCH] Initially discussed in #178. Allows adding an already encoded UTF-8 string directly without having to convert to a ``String`` first. Change-Id: I23f9c738eec18fd35f4c14f58dbd0f6cf0970dc7 --- .../com/google/flatbuffers/FlatBufferBuilder.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index 5b1425a2d..fc7cf6d6f 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -262,6 +262,21 @@ public class FlatBufferBuilder { return endVector(); } + /** + * Encode the string {@code s} in the buffer using UTF-8. + * + * @param s An already encoded UTF-8 string + * @return The offset in the buffer where the encoded string starts + */ + public int createString(ByteBuffer s) { + int length = s.remaining(); + addByte((byte)0); + startVector(1, length, 1); + bb.position(space -= length); + bb.put(s); + return endVector(); + } + /** * Should not be creating any other object, string or vector * while an object is being constructed