Java ByteBuffer到String [英] Java ByteBuffer to String

查看:135
本文介绍了Java ByteBuffer到String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是以这种方式将ByteBuffer转换为String的正确方法,

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

我问的原因是这看起来太简单了,而其他方法如Java:将字符串与ByteBuffer和相关问题进行转换看起来更多复杂。

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.

推荐答案

如果您知道字节在平台的默认字符集中,那么您的方法将是合理的。在您的示例中,这是正确的,因为 k.getBytes()返回平台默认字符集中的字节。

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.

更常见的是,您需要指定编码。但是,与您链接的问题相比,有一种更简单的方法。 String API提供了在特定编码中在String和byte []数组之间进行转换的方法。当需要对解码[编码]过程进行更多控制时,这些方法建议使用CharsetEncoder / CharsetDecoder

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."

import java.nio.charset.Charset;

要从特定编码的字符串中获取字节,可以使用兄弟的getBytes()方法:

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

byte[] bytes = k.getBytes( Charset.forName("UTF-8" ));

要将具有特定编码的字节放入String,可以使用不同的String构造函数:

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

String v = new String( bytes, Charset.forName("UTF-8") );

请注意 ByteBuffer.array()是可选操作。如果您使用数组构造了ByteBuffer,则可以直接使用该数组。否则,如果您想要安全,请使用 ByteBuffer.get(byte [] dst,int offset,int length)将缓冲区中的字节转换为字节数组。

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.

编辑

作为附带问题,在上面的示例代码中调用 Charset.forName(UTF-8)应该适用于1.4以来的所有Java版本。

As a side issue, in the sample code above the call to Charset.forName("UTF-8") should work for all versions of Java since 1.4.

如果你使用 Java 7或更高版本,您可以使用 java.nio.charset.StandardCharsets.UTF_8 (BenKirby在下面的评论中注明。)

If you're using Java 7 or later, you can instead use java.nio.charset.StandardCharsets.UTF_8. (Noted by BenKirby in his comment below.)

如果你使用 Guava ,你可以改用 com.google.common.base.Charsets.UTF_8 (由spacecamel在下面的评论中注明。)

If you're using Guava, you can instead use com.google.common.base.Charsets.UTF_8. (Noted by spacecamel in his comment below.)

这篇关于Java ByteBuffer到String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆