Reputation: 20210
Is there an approach that avoids having to copy byte[] from ByteBuffer with the ByteBuffer.get() operation.
I was looking at this post Java: Converting String to and from ByteBuffer and associated problems
and that causes an intermediary CharBuffer which I don't want as well.
I would like it to go from ByteBuffer to String.
When I know I have a byte[] underlying, this is easy with the code like so
new String(data, offset, length, charSet);
I was hoping for something similar with ByteBuffer. I am beginning to think this may not be possible? I need to decode N bytes of my ByteBuffer really.
This may be a bit of premature optimization but I am really just curious and wanted to test out the performance and squeeze every little bit out. (personal project really).
thanks, Dean
Upvotes: 0
Views: 460
Reputation: 73568
Not really for a direct ByteBuffer
, no. You need to have intermediate something, because String
doesn't take a ByteBuffer
as a constructor argument, and you can't wrap one (or even a char[]
). If the buffer is non-direct, you can use the array()
method to get a reference to the backing array (which isn't an intermediate array) and create a String
out of that.
On the plus side, there's probably a lot more performance sensitive places in your project.
Upvotes: 2