ByteBuffer to String in GWT

隐身守侯 提交于 2019-12-24 00:57:38

问题


On a PlayN project I have the following Java code

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

this works fine in Java, but when I try to compile it with GWT I get:

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

What's the proper way, in GWT, to obtain a String (encoded in UTF-8) that's inside a ByteBuffer?


回答1:


You should be able to use new String(bytes, "UTF-8") after getting the bytes out of the ByteBuffer as a byte[] using ByteBuffer#get(byte[]).
This String constructor, along with getBytes(String), is implemented for UTF-8 and ISO-8859-1.



来源:https://stackoverflow.com/questions/14141432/bytebuffer-to-string-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!