mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-04 21:11:10 +00:00
@@ -17,6 +17,11 @@
|
|||||||
package com.google.flatbuffers;
|
package com.google.flatbuffers;
|
||||||
|
|
||||||
import static com.google.flatbuffers.Constants.*;
|
import static com.google.flatbuffers.Constants.*;
|
||||||
|
|
||||||
|
import java.nio.CharBuffer;
|
||||||
|
import java.nio.charset.CharacterCodingException;
|
||||||
|
import java.nio.charset.CharsetEncoder;
|
||||||
|
import java.nio.charset.CoderResult;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
@@ -45,6 +50,8 @@ public class FlatBufferBuilder {
|
|||||||
int num_vtables = 0; // Number of entries in `vtables` in use.
|
int num_vtables = 0; // Number of entries in `vtables` in use.
|
||||||
int vector_num_elems = 0; // For the current vector being built.
|
int vector_num_elems = 0; // For the current vector being built.
|
||||||
boolean force_defaults = false; // False omits default values from the serialized data.
|
boolean force_defaults = false; // False omits default values from the serialized data.
|
||||||
|
CharsetEncoder encoder = utf8charset.newEncoder();
|
||||||
|
ByteBuffer dst;
|
||||||
/// @endcond
|
/// @endcond
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -367,12 +374,26 @@ public class FlatBufferBuilder {
|
|||||||
* @return The offset in the buffer where the encoded string starts.
|
* @return The offset in the buffer where the encoded string starts.
|
||||||
*/
|
*/
|
||||||
public int createString(String s) {
|
public int createString(String s) {
|
||||||
byte[] utf8 = s.getBytes(utf8charset);
|
int length = s.length();
|
||||||
addByte((byte)0);
|
int estimatedDstCapacity = (int) (length * encoder.maxBytesPerChar());
|
||||||
startVector(1, utf8.length, 1);
|
if (dst == null || dst.capacity() < estimatedDstCapacity) {
|
||||||
bb.position(space -= utf8.length);
|
dst = ByteBuffer.allocate(Math.max(128, estimatedDstCapacity));
|
||||||
bb.put(utf8, 0, utf8.length);
|
}
|
||||||
return endVector();
|
|
||||||
|
dst.clear();
|
||||||
|
|
||||||
|
CharBuffer src = CharBuffer.wrap(s);
|
||||||
|
CoderResult result = encoder.encode(src, dst, true);
|
||||||
|
if (result.isError()) {
|
||||||
|
try {
|
||||||
|
result.throwException();
|
||||||
|
} catch (CharacterCodingException x) {
|
||||||
|
throw new Error(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dst.flip();
|
||||||
|
return createString(dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user