3

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?

2
  • What is paramByteBuffer? And what is in it? Commented May 21, 2015 at 3:05
  • Its a byte buffer data, But not sure of the format. Commented May 21, 2015 at 11:19

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

6

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);

3 Comments

Still, its returning only some junk symbol.
You should expect that. Different charsets have different values for each character.
You need to know which charset the data has been encoded with.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.