ByteBuffer转String的方法
来源:互联网 发布:kali linux攻击服务器 编辑:程序博客网 时间:2024/06/15 16:49
ByteBuffer俗称缓冲器,是将数据移进移出通道的唯一方式,并且我们只能创建一个独立的基本类型缓冲器,或者使用”as”方法从ByteBuffer中获得。
ByteBuffer中存放的是字节数组,如果要将它们转换成字符串则需要使用 Charset,Charset是字符编码,它提供了把字节流转换成字符串 ( 解码 ) 和将字符串转换成字节流 ( 编码) 的方法。
HBase中读出的数据都是以ByteBuffer形式进行传输的,因此设计到ByteBuffer转换成String输出。
以下是实现该过程的方法:
public static String byteBufferToString(ByteBuffer buffer) {
CharBuffer charBuffer = null;
try {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
charBuffer = decoder.decode(buffer);
buffer.flip();
return charBuffer.toString();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
需要说明的是,flip方法会将缓冲区的当前位置”重置”到缓冲区的开始处.这样可以保证传入的对像被其他方法使用时读取的数据完整性.
- ByteBuffer转String的方法
- java标准库中ByteBuffer转String的方法
- ByteBuffer转String
- ByteBuffer和String的互相转换
- ByteBuffer.allocate()与ByteBuffer.allocateDirect()方法的区别。
- ByteBuffer 转化为 String
- String ByteBuffer转换
- String ByteBuffer转换
- String和ByteBuffer互转
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer.mark()方法
- ByteBuffer常用方法详解
- ByteBuffer常用方法详解
- ByteBuffer 转换为String类型
- tomcat7 - cacti 备忘
- 群集学习
- Linux系统学习笔记:链接
- 调用Android手机自带的闪光灯
- 解决Python报错:local variable 'xxx' referenced before assignment(引)
- ByteBuffer转String的方法
- Linux2.4-net源码学习笔记 IP层协议栈的实现(II IP数据报路由)
- MyEclipse使用经验总结
- git for windows (又名 msysgit)如何记住用户名和密码 (亲测)
- Linux系统学习笔记:异常控制流
- cgi error:用iis运行php5.3以下版本报cgi错误
- Windows Phone 加速度计的使用
- Linux系统学习笔记:存储器
- POJ 3162 Walking Race (DFS + 线段树)