ByteBuffer to String in GWT



在PlayN项目中,我有以下Java代码

import com.google.common.base.Charsets;
import java.nio.ByteBuffer;
ByteBuffer msg = ... // a ByteBuffer that contains a String
String s = Charsets.UTF_8.decode(msg).toString();

这在Java中运行良好,但当我尝试用GWT编译它时,我得到了:

The method decode(ByteBuffer) is undefined for the type Charset

在GWT中,获取ByteBuffer中的String(以UTF-8编码(的正确方法是什么?

在使用ByteBuffer#get(byte[])将字节从ByteBuffer中取出后,您应该能够使用new String(bytes, "UTF-8")作为byte[]
这个String构造函数和getBytes(String)是针对UTF-8和ISO-8859-1实现的。

最新更新