160

Is this a correct approach to convert ByteBuffer to String in this way,

String k = "abcd";
ByteBuffer b = ByteBuffer.wrap(k.getBytes());
String v = new String(b.array());

if(k.equals(v))
    System.out.println("it worked");
else
    System.out.println("did not work");

The reason I ask is that is this looks too simple, whereas other approaches like Java: Converting String to and from ByteBuffer and associated problems looks more complex.

Community
  • 1
  • 1
vikky.rk
  • 3,989
  • 5
  • 29
  • 32
  • 3
    Well, did you try it? – tckmn Jun 27 '13 at 23:30
  • 6
    Yes I did and it works. But I have seen other implementations which are more complex, like http://stackoverflow.com/questions/1252468/java-converting-string-to-and-from-bytebuffer-and-associated-problems – vikky.rk Jun 27 '13 at 23:32
  • 1
    @Doorknob et. al. He's missing encoding and his example (when syntax is corrected) will work, but his method is still not right. – Gus Jun 27 '13 at 23:47

12 Answers12

163

There is simpler approach to decode a ByteBuffer into a String without any problems, mentioned by Andy Thomas.

String s = StandardCharsets.UTF_8.decode(byteBuffer).toString();
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
xinyong Cheng
  • 1,786
  • 1
  • 10
  • 6
  • 6
    Be aware that UTF-8 might not be the optimal charset for converting bytes to strings and vice-versa. For a 1-to-1 mapping of bytes to chars better use ISO-8859-1, see https://stackoverflow.com/questions/9098022/problems-converting-byte-array-to-string-and-back-to-byte-array. – asmaier May 10 '17 at 08:53
  • 2
    Also, of you don't *really* need a string, the `CharBuffer` `decode()` returns is a `CharSequence` (like `String`), so you can avoid an extra copy and use it directly. – David Ehrmann Aug 01 '18 at 18:05
  • @DavidEhrmann CharBuffer does not have a decode method - what are you referring to here? – Tom Anderson Nov 23 '21 at 15:12
  • @TomAnderson `Charset` has a `decode()` method. – David Ehrmann Nov 24 '21 at 00:59
  • 1
    @DavidEhrmann Ah! I completely misparsed your comment, sorry! – Tom Anderson Nov 26 '21 at 15:22
90

EDIT (2018): The edited sibling answer by @xinyongCheng is a simpler approach, and should be the accepted answer.

Your approach would be reasonable if you knew the bytes are in the platform's default charset. In your example, this is true because k.getBytes() returns the bytes in the platform's default charset.

More frequently, you'll want to specify the encoding. However, there's a simpler way to do that than the question you linked. The String API provides methods that converts between a String and a byte[] array in a particular encoding. These methods suggest using CharsetEncoder/CharsetDecoder "when more control over the decoding [encoding] process is required."

To get the bytes from a String in a particular encoding, you can use a sibling getBytes() method:

byte[] bytes = k.getBytes( StandardCharsets.UTF_8 );

To put bytes with a particular encoding into a String, you can use a different String constructor:

String v = new String( bytes, StandardCharsets.UTF_8 );

Note that ByteBuffer.array() is an optional operation. If you've constructed your ByteBuffer with an array, you can use that array directly. Otherwise, if you want to be safe, use ByteBuffer.get(byte[] dst, int offset, int length) to get bytes from the buffer into a byte array.

blong
  • 2,815
  • 8
  • 44
  • 110
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • and in the `ByteBuffer.get` function, the input is again an array of bytes, how can I get it? it doesn't make any sense to say again k.getbytes, does it? – William Kinaan Jul 24 '15 at 13:49
  • @WilliamKinaan - You have the byte[] you fed to `ByteBuffer.get(byte[] dst, int offset, int length)`. You you can build a String out of it with the String() constructor `String(byte[] bytes, int offset, int length, Charset charset). You can use the same offset and length values for both calls. – Andy Thomas Jul 24 '15 at 14:55
  • 1
    There's no k.getBytes() method in java.nio.ByteBuffer (may be not in the version am using). So I used k.array() method which will return byte[]. – Madura Pradeep Apr 20 '16 at 09:29
  • @MaduraPradeep - In the example code in the question and this answer, `k` is a String, not a ByteBuffer. – Andy Thomas Apr 20 '16 at 13:14
  • Be aware that UTF-8 might not be the optimal charset for converting bytes to strings and vice-versa. For a 1-to-1 mapping of bytes to chars better use ISO-8859-1, see https://stackoverflow.com/questions/9098022/problems-converting-byte-array-to-string-and-back-to-byte-array – asmaier May 08 '17 at 16:55
  • There is yet another problem with this solution that bit me. The ByteBuffer has an arrayOffset(). Normally, this only happens when you slice() a Buffer, but Android was allocating() buffers with an offset! Extremely hard issue to find. – Dustin Oct 05 '20 at 13:59
17

Try this:

new String(bytebuffer.array(), "ASCII");

NB. you can't correctly convert a byte array to a String without knowing its encoding.

I hope this helps

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
  • 11
    UTF-8 is probably a better default guess than ASCII? – Gus Jun 27 '13 at 23:37
  • 3
    Neither should be specified, given the OP's use of k.getBytes(), which uses the platform's default charset. – Andy Thomas Jun 27 '13 at 23:55
  • 8
    Not all buffers are backed by an array, so `.array()` may throw an exception. – Dzmitry Lazerka Jan 27 '16 at 23:35
  • 1
    Not all bytebuffers support the `.array()` method. – ScalaWilliam May 13 '17 at 09:12
  • new String(buffer.array(), 0, length, "UTF-8") good approach would be to specify lengths, start and end – Mahesh Pujari Jul 18 '18 at 04:47
  • 3
    Careful! If you use `array()`, you _must_ also use `arrayOffset()` to start at the correct position in the array! This is a subtle pitfall, because usually arrayOffset() is 0; but in those rare cases where it isn't you will get hard-to-find bugs if you don't take it into account. – oliver Jul 18 '18 at 09:41
  • This was the only method that worked for me on a `java.nio.ByteBuffer` instance. ```String fileContent = new String(bb.array(), StandardCharsets.UTF_8);``` – Binita Bharati Jul 29 '21 at 11:00
15

Just wanted to point out, it's not safe to assume ByteBuffer.array() will always work.

byte[] bytes;
if(buffer.hasArray()) {
    bytes = buffer.array();
} else {
    bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
}
String v = new String(bytes, charset);

Usually buffer.hasArray() will always be true or false depending on your use case. In practice, unless you really want it to work under any circumstances, it's safe to optimize away the branch you don't need. But the rest of the answers may not work with a ByteBuffer that's been created through ByteBuffer.allocateDirect().

Fuwjax
  • 2,327
  • 20
  • 18
  • If the buffer is created via `ByteBuffer.wrap(bytes, offset, size)` factory `.array()` will return the entire `bytes` array. Better use the form xinyong Cheng suggested – Lev Kuznetsov Feb 23 '17 at 03:33
  • The .decode() on Charset is a better solution, agreed. I do feel the context of my answer is useful information, but much less so now. – Fuwjax Feb 24 '17 at 05:09
  • 2
    Careful! If you use `array()`, you _must_ also use `arrayOffset()` to start at the correct position in the array! This is a subtle pitfall, because usually arrayOffset() is 0; but in those rare cases where it isn't you will get hard-to-find bugs if you don't take it into account. – oliver Jul 18 '18 at 09:42
8

The answers referring to simply calling array() are not quite correct: 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.

Alex Yarmula
  • 10,477
  • 5
  • 33
  • 32
2

the root of this question is how to decode bytes to string?

this can be done with the JAVA NIO CharSet:

public final CharBuffer decode(ByteBuffer bb)

FileChannel channel = FileChannel.open(
  Paths.get("files/text-latin1.txt", StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
channel.read(buffer);

CharSet latin1 = StandardCharsets.ISO_8859_1;
CharBuffer latin1Buffer = latin1.decode(buffer);

String result = new String(latin1Buffer.array());
  • First we create a channel and read it in a buffer
  • Then decode method decodes a Latin1 buffer to a char buffer
  • We can then put the result, for instance, in a String
Eric Bolinger
  • 2,722
  • 1
  • 13
  • 22
宏杰李
  • 11,820
  • 2
  • 28
  • 35
  • Your code is not decoding from latin1 to utf8. While your code is correct, calling the CharBuffer utf8Buffer is somewhat misleading because it has no encoding. – Björn Lindqvist Jan 24 '19 at 05:14
1

Convert a String to ByteBuffer, then from ByteBuffer back to String using Java:

import java.nio.charset.Charset;
import java.nio.*;

String babel = "obufscate thdé alphebat and yolo!!";
System.out.println(babel);
//Convert string to ByteBuffer:
ByteBuffer babb = Charset.forName("UTF-8").encode(babel);
try{
    //Convert ByteBuffer to String
    System.out.println(new String(babb.array(), "UTF-8"));
}
catch(Exception e){
    e.printStackTrace();
}

Which prints the printed bare string first, and then the ByteBuffer casted to array():

obufscate thdé alphebat and yolo!!
obufscate thdé alphebat and yolo!!

Also this was helpful for me, reducing the string to primitive bytes can help inspect what's going on:

String text = "こんにちは";
//convert utf8 text to a byte array
byte[] array = text.getBytes("UTF-8");
//convert the byte array back to a string as UTF-8
String s = new String(array, Charset.forName("UTF-8"));
System.out.println(s);
//forcing strings encoded as UTF-8 as an incorrect encoding like
//say ISO-8859-1 causes strange and undefined behavior
String sISO = new String(array, Charset.forName("ISO-8859-1"));
System.out.println(sISO);

Prints your string interpreted as UTF-8, and then again as ISO-8859-1:

こんにちは
ããã«ã¡ã¯
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
0

Notice (aside from the encoding issue) that some of the more complicated code linked goes to the trouble of getting the "active" portion of the ByteBuffer in question (for example by using position and limit), rather than simply encoding all of the bytes in the entire backing array (as many of the examples in these answers do).

Jas
  • 327
  • 2
  • 9
0
private String convertFrom(String lines, String from, String to) {
    ByteBuffer bb = ByteBuffer.wrap(lines.getBytes());
    CharBuffer cb = Charset.forName(to).decode(bb);
    return new String(Charset.forName(from).encode(cb).array());
};
public Doit(){
    String concatenatedLines = convertFrom(concatenatedLines, "CP1252", "UTF-8");
};
0

Here is a simple function for converting a byte buffer to string:

public String byteBufferToString(ByteBuffer bufferData) {
    byte[] buffer = new byte[bufferData.readableByteCount()];
    // read bufferData and insert into buffer 
    data.read(buffer);
    // CharsetUtil supports UTF_16, ASCII, and many more
    String text = new String(buffer, CharsetUtil.UTF_8);
    System.out.println("Text: "+text);
    return text;
}
Jitendra Asawa
  • 103
  • 1
  • 5
0

This was the only method that worked for me on a java.nio.ByteBuffer instance:

String fileContent = new String(bb.array(), StandardCharsets.UTF_8);

Related code snippet below:

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;


Path path = Paths.get("/home/binita/testbb");
FileChannel fileChannel = FileChannel.open(path, 
                 EnumSet.of(StandardOpenOption.READ
                    )
                 );  
            
ByteBuffer bb = ByteBuffer.allocate(1024);
int bytesRead = fileChannel.read(bb);
if(bytesRead > 0) {
 String fileContent = new String(bb.array(), StandardCharsets.UTF_8);
}
Binita Bharati
  • 5,239
  • 1
  • 43
  • 24
0

May be toooo late but here is a solution that works for me.

fun byteBufferToByteString(byteBuffer: ByteBuffer) : ByteString {
        var size = byteBuffer.capacity()
        byteBuffer.position(0)        
        return ByteString.copyFrom(byteBuffer,size)
}

Note: Setting buffer size is important here , as without that, it might give error while converting the ByteString back to bytearray at receivers end.

nutts786
  • 1
  • 3