Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a String from ByteBuffer without double buffering

Is there a way to construct a String from ByteBuffer without first reading contents from the buffer to a intermediate byte[] or char[]?

An API similar to the string constructor that takes a byte[] strikes me as ideal:

public String(ByteBuffer buffer, int offset, int length, Charset charset)

... but no such thing exists.

I've found How to convert from ByteBuffer to Integer and String?, but it uses an auxiliary array.

I the next best thing I've found so far is to project the byte buffer as a CharBuffer and call toString(). But that doesn't allow for compressing the strings with something like UTF-8.

like image 611
Dilum Ranatunga Avatar asked Dec 03 '25 18:12

Dilum Ranatunga


1 Answers

What about CharsetDecoder.decode and calling toString on the returned CharBuffer.