It seems that String.getBytes()
will create a new byte array, so there is an extra memory copy. Can I encode a String directly to a ByteBuffer
without an intermediate byte array?
for example:
void putString(ByteBuffer bb, String s) {
byte[] arr = s.getBytes(StandardCharsets.UTF_8);
bb.put(arr);
}
This piece of code will create a byte array, encode the string to this byte array, then copy the content of byte array to ByteBuffer. I think the byte array is not necessary, it will bring GC and extra memory copy.