Reputation: 45
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.
Upvotes: 2
Views: 987
Reputation: 50726
You can use CharsetEncoder
to write directly to a ByteBuffer
:
static void putString(ByteBuffer buffer, String str, Charset charset) {
CharsetEncoder encoder = charset.newEncoder();
encoder.encode(CharBuffer.wrap(str), buffer, true);
encoder.flush(buffer);
}
It's your responsibility to make sure enough space has been allocated. You can also check the result of the encode()
method to see if it was successful.
Upvotes: 4
Reputation: 41243
I can't think of a simple way to completely eliminate intermediate byte arrays.
However if you're worrying about this because the String is huge, you can break it into chunks:
for(offset=0; offset<str.length(); offset+=chunkSize) {
String chunk = str.substring(offset, offset+chunkSize);
byteBuffer.put(chunk.getBytes(StandardCharsets.UTF_8));
}
However, if your input strings are huge enough that this optimisation is necessary, the overall architecture of your program is probably ill-conceived.
You should not worry about GC performance unless you've seen something unusual while profiling. The JRE is brilliant at efficient GC.
Upvotes: 1
Reputation: 140475
String objects are immutable by purpose. The whole idea of that class is to not allow to manipulate any underlying data structures (mainly for security and performance optimization reasons).
In that sense: there is no other better approach to acquire bytes making up a string object in Java.
Upvotes: 0