2

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.

4 Answers4

4

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.

shmosel
  • 49,289
  • 6
  • 73
  • 138
1

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.

slim
  • 40,215
  • 13
  • 94
  • 127
0

No, it is not possible. String objects don't have encoding.

Bas
  • 4,423
  • 8
  • 36
  • 53
0

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248