【问题标题】:ByteBuffer to String in GWTByteBuffer 到 GWT 中的字符串
【发布时间】:2013-01-03 15:03:13
【问题描述】:

在 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 中的字符串(以 UTF-8 编码)的正确方法是什么?

【问题讨论】:

标签: gwt playn bytebuffer


【解决方案1】:

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

【讨论】:

  • 我一直在寻找一个非常复杂的解决方案,实际上就是这么简单......谢谢!
  • 如果您查看 gwt 超级源代码,您会看到 String 默认为 UTF-8;但是,如果这是在共享代码中,Thomas 的答案是跨平台的正确解决方案。 Nit:Gwt 还支持使用“ISO-LATIN-1”的拉丁语 :)
猜你喜欢
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 2010-11-17
  • 1970-01-01
相关资源
最近更新 更多