ByteBuffer在GWT中的字符串 [英] ByteBuffer to String in GWT

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

问题描述

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

  import com.google.common.base.Charsets; 
import java.nio.ByteBuffer;

ByteBuffer msg = ... //一个包含字符串的ByteBuffer
String s = Charsets.UTF_8.decode(msg).toString();

这在Java中工作正常,但是当我尝试使用GWT进行编译时,我得到:



方法decode(ByteBuffer)对于Charset类型是未定义的



在GWT中,获得一个ByteBuffer内部的字符串(用UTF-8编码)的正确方法是什么?

解决方案

在从 ByteBuffer 新字符串(字节,UTF-8) c>作为字节[] 使用 ByteBuffer#get(byte [])

这个 String 构造函数以及 getBytes(String)是为UTF-8和ISO-8859-1实现的。

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?

解决方案

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.

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

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