Liam de Haas
Liam de Haas

Reputation: 1268

Convert ByteBuffer to String in Java

I have a byte[] bytes from a ByteBuffer which contains a packet. I want to put the packet in a String.

Currently I have the following

    byte[] bytes = packet.array();
    System.out.println("Packet String:" + new String(bytes));

But then I end up with the following output

Packet String:E����<Ҧ@��@�.
03-22 04:30:28.187   9296-10152/willem.com.vpn I/System.out﹕ ����J}�j���k�:����������9�������
03-22 04:30:28.197   9296-10152/willem.com.vpn I/System.out﹕ ��&�4��������ddrarpa��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

I've tried it with encoding like this

System.out.println("Packet String:" + new String(bytes, Charset.forName("UTF-8")));

But that isn't the right charset. Can anybody tell me what is?

Upvotes: 8

Views: 20993

Answers (4)

chinthakad
chinthakad

Reputation: 979

ByteBuffer data = ...

new String(data.array() , "UTF-8");

Upvotes: -1

user207421
user207421

Reputation: 311039

You're ignoring the limit of the buffer. You should flip() the buffer, then call

new String(buffer.array(), 0, buffer.position())

Then reset it.

But you should really be using a Charset decoder to yield a CharBuffer for this. At least you should specify a character set to the String constructor.

Upvotes: 0

Alex Yarmula
Alex Yarmula

Reputation: 10677

The answer talking about setting range from position to limit is not correct in a general case. When the buffer has been partially consumed, or is referring to a part of an array (you can ByteBuffer.wrap an array at a given offset, not necessarily from the beginning), we have to account for that in our calculations. This is the general solution that works for buffers in all cases (does not cover encoding):

if (myByteBuffer.hasArray()) {
    return new String(myByteBuffer.array(),
        myByteBuffer.arrayOffset() + myByteBuffer.position(),
        myByteBuffer.remaining());
} else {
    final byte[] b = new byte[myByteBuffer.remaining()];
    myByteBuffer.duplicate().get(b);
    return new String(b);
}

For the concerns related to encoding, see Andy Thomas' answer.

Upvotes: 1

kuujo
kuujo

Reputation: 8195

You need to use the buffer's position and limit to determine the number of bytes to read.

// ...populate the buffer...
buffer.flip(); // flip the buffer for reading
byte[] bytes = new byte[buffer.remaining()]; // create a byte array the length of the number of bytes written to the buffer
buffer.get(bytes); // read the bytes that were written
String packet = new String(bytes);

In my opinion you shouldn't really be using the backing array() at all; it's bad practice. Direct byte buffers (created by ByteBuffer.allocateDirect() won't have a backing array and will throw an exception when you try to call ByteBuffer.array(). Because of this, for portability you should try to stick to the standard buffer get and put methods. Of course, if you really want to use the array you can use ByteBuffer.hasArray() to check if the buffer has a backing array.

Upvotes: 12

Related Questions