CharBuffer类编码相关问题

CharBuffer是java.nio下面的一个字符缓存类,一般情况下,我么用不到它。平时都会用ByteBuffer代替。

 

没事的时候,研究一下,发现CharBuffer在编码转换上有奇怪的问题。

 

 

获取CharBuffer对象方法有

ByteBuffer byteBuffer=ByteBuffer.allocate(1024);

 

byteBuffer.put("测试字符aabc".getBytes());

 

 

byteBuffer.asCharBuffer();

CharBuffer charBuffer=CharBuffer.wrap();

CharBuffer charBuffer=CharBuffer.allocate();

 

使用误区:

有人认为可以把ByteBuffer转换为字符串,直接通过下面的方法。

 

byteBuffer.asCharBuffer().toString();

 

这样是错误的。(除非你存放在byteBuffer中的字节是utf-16be,就可以正常输出)

 

如果存放在byteBuffer中的字节不是utf-16be的,那么通过asCharBuffer方法转换为CharBuffer后,怎么正确输出。

 

测试代码如下:

package com.cgb.train.core.nio;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class CharBufferTest2 {

	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		
		String s="我们abc";
		ByteBuffer byteBuffer1=ByteBuffer.allocate(50);
		ByteBuffer byteBuffer2=ByteBuffer.allocate(50);
		
		byteBuffer1.put(s.getBytes("UTF-16BE"));//使用utf-16be字符编码
		byteBuffer1.flip();//注意put后,position会发生位置改变。为读做好准备。
		byteBuffer2.put(s.getBytes());//使用默认字符编码
		byteBuffer2.flip();
		
		System.out.println(byteBuffer1.asCharBuffer().toString());
		/**
		 * 下面输出乱码
		 */
		System.out.println(byteBuffer2.asCharBuffer().toString());
		/**
		 * 或者直接用下面的方式输出,也是正常的。
		 * 
		 * ByteBuffer.wrap(s.getBytes执行后,不需要flip,已经为读做好准备了。
		 * 
		 */
		System.out.println(ByteBuffer.wrap(s.getBytes("UTF-16BE")).asCharBuffer().toString());
	}

}

 

 

所以对于ByteBuffer中存放UTF-16BE编码的字节时,可以直接使用asCharBuffer()直接转换成字符缓冲。

 

那么对于不是存放UTF-16BE编码的ByteBuffer,需要使用

CharBuffer charBuffer=Charset.forName("字符编码").decode(byteBuffer)进行转换。

 

注意事项:

1、对ByteBuffer字节缓冲的视图缓冲例如:CharBuffer、IntBuffer、LongBuffer等进行操作引起位置变化的时候,不会引起底层的ByteBuffer的位置(position,limit,mark等)变化。

2、注意有几个带索引参数的添加数据put(index)和获取数据get(index)方法,是不会改变位置的。 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值