1. Overview
1.概述
ByteBuffer is one of many beneficial classes in the java.nio package. It’s used to read data from channels and write data into channels directly.
ByteBuffer是java.nio包中众多有益的类之一。它被用来直接从通道中读取数据和向通道中写入数据。
In this short tutorial, we’ll learn how to convert a ByteBuffer to String in Java.
在这个简短的教程中,我们将学习如何在Java中将一个ByteBuffer转换为String。
2. Converting a ByteBuffer to String
2.将ByteBuffer转换为String
The process of converting a ByteBuffer to a String is decoding. This process requires a Charset.
将ByteBuffer转换为String的过程就是解码。这个过程需要一个Charset。
There are three ways to convert a ByteBuffer to String:
有三种方法可以将ByteBuffer转换为String。
- creating a new String from bytebuffer.array()
- creating a new String from bytebuffer.get(bytes)
- using charset.decode()
We will use a simple example to showcase all the three ways of converting a ByteBuffer to a String.
我们将使用一个简单的例子来展示将ByteBuffer转换为String的所有三种方式。
3. Practical Example
3.实例
3.1. Creating a New String From bytebuffer.array()
3.1.从bytebuffer.array()创建一个新的String
The first step is to get the byte array from ByteBuffer. To do that, we’ll call the ByteBuffer.array() method. This will return the backing array.
第一步是从ByteBuffer中获取字节数组。要做到这一点,我们将调用ByteBuffer.array()方法。这将返回支持的数组。
Then, we can call the String constructor, which accepts a byte array and character encoding to create our new String:
然后,我们可以调用String构造函数,它接受一个字节数组和字符编码来创建我们新的String。
@Test
public void convertUsingNewStringFromBufferArray_thenOK() {
String content = "baeldung";
ByteBuffer byteBuffer = ByteBuffer.wrap(content.getBytes());
if (byteBuffer.hasArray()) {
String newContent = new String(byteBuffer.array(), charset);
assertEquals(content, newContent);
}
}
3.2. Creating a New String From bytebuffer.get(bytes)
3.2.从bytebuffer.get(bytes)创建一个新的字符串
In Java, we can use new String(bytes, charset) to convert a byte[] to a String.
在Java中,我们可以使用new String(bytes, charset)来将byte[]转换为String。
For character data, we can use the UTF_8 charset to convert a byte[] to a String. However, when byte[] is holding non-text binary data, the best practice is to convert the byte[] into a Base64 encoded String:
对于字符数据,我们可以使用UTF_8字符集来将byte[]转换为String。然而,当byte[]持有非文本二进制数据时,最好的做法是将byte[]转换为Base64编码 String。
@Test
public void convertUsingNewStringFromByteBufferGetBytes_thenOK() {
String content = "baeldung";
ByteBuffer byteBuffer = ByteBuffer.wrap(content.getBytes());
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
String newContent = new String(bytes, charset);
assertEquals(content, newContent);
}
3.3. Using charset.decode()
3.3.使用charset.decode()
This is the simplest way to convert a ByteBuffer into a String without any problems:
这是将ByteBuffer转换成String的最简单方法,没有任何问题。
@Test
public void convertUsingCharsetDecode_thenOK() {
String content = "baeldung";
ByteBuffer byteBuffer = ByteBuffer.wrap(content.getBytes());
String newContent = charset.decode(byteBuffer).toString();
assertEquals(content, newContent);
}
4. Conclusion
4.总结
We have learned in this tutorial three ways to convert ByteBuffer to String in Java. Just remember to use the proper character encoding, and in our example, we used UTF-8.
我们在本教程中学习了三种在Java中将ByteBuffer转换成String的方法。只要记住使用适当的字符编码,在我们的例子中,我们使用了UTF-8。
As always, the complete source code of the examples is available over on GitHub.
一如既往,这些例子的完整源代码可在GitHub上获得over。