How to convert ByteBuffer to String in Java [Example] (original) (raw)

You can easily convert ByteBuffer to String in Java if you know how to convert byte array to String. Why? because it's very easy to convert ByteBuffer to a byte array and vice versa. All you need to do is call the ByteBuffer.array() method, it will return you the byte array used by java.nio.ByteBuffer class, later you can easily create String from that byte array. Though always remember to provide correct character encoding while converting byte array to String.

For example, if you know that ByteBuffer is filled with bytes encoded in UTF-8 then you must use the same encoding while creating String from that byte array. String class provides an overloaded constructor which accepts character encoding along with byte array.

You can use the snippet shared in this example to do the job. ByteBuffer is one of the very useful classes in java.nio package which is used to read data from channels and write data into channels directly.

Same ByteBuffer can be used to read and write data. If you want to read from ByteBuffer just call the flip() method and it will convert ByteBuffer into reading mode. In this article, you will learn how to convert ByteBuffer to String in Java. I have given a simple example, but if you still any doubt you can always leave a comment or question.

ByteBuffer to String in Java - Example

This is one of the simplest examples to demonstrate how to convert ByteBuffer to String. In this Java program, I have first created a String literal, then I have got the byte array from that String in the UTF-8 encoding scheme.

After that, I have created a ByteBuffer object by using the same byte array and using ByteBuffer.wrap() method. This is exactly the same situation when your ByteBuffer is filled with data whether you read it from the network or a RandomAccessFile in Java. I have just used this direct approach to create a ByteBuffer with data to simplify this example.

The next part of this program is most important because there we are converting ByteBuffer to String. In order to get the byte array from ByteBuffer just call the ByteBuffer.array() method. This method will return the backed array.

Now you can call the String constructor which accepts a byte array and character encoding to create String. You are done.

ByteBuffer to String in Java with Example

here is our complete code example for converting ByteBuffer to String :

import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer;

/**

}

Output : Original : It's easy to convert ByteBuffer to String in Java Converted : It's easy to convert ByteBuffer to String in Java

That's all about how to convert ByteBuffer to String in Java. Just remember to use the right character encoding because without using proper character encoding there is no guarantee that you will get the original string back. Though UTF-8 is a good default it cannot be used to provide guaranteed.

If you like this article and want to learn more about converting one type to another in Java, you can also check the following Java tutorials :