Converting bytebuffer to string not working in java
Solution 1
Buffers are a little tricky to use as they have a current state, which you need to take into account when accessing them.
you want to put
paramByteBuffer.flip();
before each decode to get the buffer into the state you want for the decode to read.
e.g.
ByteBuffer paramByteBuffer = ByteBuffer.allocate(100);
paramByteBuffer.put((byte)'a'); // write 'a' at next position(0)
paramByteBuffer.put((byte)'b'); // write 'b' at next position(1)
paramByteBuffer.put((byte)'c'); // write 'c' at next position(2)
// if I try to read now I will read the next byte position(3) which is empty
// so I need to flip the buffer so the next position is at the start
paramByteBuffer.flip();
// we are at position 0 so we can do our read function
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(paramByteBuffer);
String text = charBuffer.toString();
System.out.println("UTF-8" + text);
// because the decoder has read all the written bytes we are back to the
// state (position 3) we had just after we wrote the bytes in the first
// place so we need to flip again
paramByteBuffer.flip();
// we are now at position 0 so we can do our read function
charBuffer = StandardCharsets.UTF_16.decode(paramByteBuffer);
text = charBuffer.toString();
System.out.println("UTF_16"+text);
Solution 2
You can do something like this:
String val = new String(paramByteBuffer.array());
OR
String val = new String(paramByteBuffer.array(),"UTF-8");
Here is a list of supported charsets
Solution 3
The toString
method of HeapByteBuffer
simply
Returns a string summarizing the state of this buffer.
In other words, it returns
java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
which shows the byte buffer's position, limit, and capacity.
In this case, your ByteBuffer
has capacity for 3 more bytes. Each Charset#decode
call consumes the ByteBuffer
and you don't rewind/reset it so there are no more bytes to consume for subsequent calls. In other words, all those strings will be empty.
Harry
Updated on June 05, 2022Comments
-
Harry about 2 years
In runtime am getting a bytebuffer data from a device, Am trying to decode that data to read the contents of it.
When I print the bytebuffer using string it shows as follows,
java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
I tried to decode using all the known formats as follows,
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(paramByteBuffer); String text = charBuffer.toString(); System.out.println("UTF-8"+text); charBuffer = StandardCharsets.UTF_16.decode(paramByteBuffer); text = charBuffer.toString(); System.out.println("UTF_16"+text); charBuffer = StandardCharsets.ISO_8859_1.decode(paramByteBuffer); text = charBuffer.toString(); System.out.println("ISO_8859_1"+text); charBuffer = StandardCharsets.UTF_16BE.decode(paramByteBuffer); text = charBuffer.toString(); System.out.println("UTF_16BE"+text); charBuffer = StandardCharsets.UTF_16LE.decode(paramByteBuffer); text = charBuffer.toString(); System.out.println("UTF_16LE"+text); charBuffer = StandardCharsets.US_ASCII.decode(paramByteBuffer); text = charBuffer.toString(); System.out.println("US_ASCII"+text);
Everything returns the empty data.
What are the ways to decode the byte buffer data?