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.
What about CharsetDecoder.decode and calling toString on the returned CharBuffer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With