Java: StringBuffer to byte[] without toString
As many have already suggested, you can use the CharBuffer class, but allocating a new CharBuffer would only make your problem worse.
Instead, you can directly wrap your StringBuilder in a CharBuffer, since StringBuilder implements CharSequence:
Charset charset = StandardCharsets.UTF_8;CharsetEncoder encoder = charset.newEncoder();// No allocation performed, just wraps the StringBuilder.CharBuffer buffer = CharBuffer.wrap(stringBuilder);ByteBuffer bytes = encoder.encode(buffer);
EDIT: Duarte correctly points out that the CharsetEncoder.encode
method may return a buffer whose backing array is larger than the actual data—meaning, its capacity is larger than its limit. It is necessary either to read from the ByteBuffer itself, or to read a byte array out of the ByteBuffer that is guaranteed to be the right size. In the latter case, there's no avoiding having two copies of the bytes in memory, albeit briefly:
ByteBuffer byteBuffer = encoder.encode(buffer);byte[] array;int arrayLen = byteBuffer.limit();if (arrayLen == byteBuffer.capacity()) { array = byteBuffer.array();} else { // This will place two copies of the byte sequence in memory, // until byteBuffer gets garbage-collected (which should happen // pretty quickly once the reference to it is null'd). array = new byte[arrayLen]; byteBuffer.get(array);}byteBuffer = null;
If you're willing to replace the StringBuilder
with something else, yet another possibility would be a Writer
backed by a ByteArrayOutputStream
:
ByteArrayOutputStream bout = new ByteArrayOutputStream();Writer writer = new OutputStreamWriter(bout);try { writer.write("String A"); writer.write("String B");} catch (IOException e) { e.printStackTrace();}System.out.println(bout.toByteArray());try { writer.write("String C");} catch (IOException e) { e.printStackTrace();}System.out.println(bout.toByteArray());
As always, your mileage may vary.
For starters, you should probably be using StringBuilder
, since StringBuffer
has synchronization overhead that's usually unnecessary.
Unfortunately, there's no way to go directly to byte
s, but you can copy the char
s into an array or iterate from 0
to length()
and read each charAt()
.