Charset.encode(buffer)之后,buffer的变化

来源:互联网 发布:特效照片制作软件 编辑:程序博客网 时间:2024/05/01 00:55
import java.nio.charset.Charset;import java.nio.charset.CharsetEncoder;import java.nio.charset.CharsetDecoder;import java.nio.CharBuffer;import java.nio.Buffer;import java.nio.ByteBuffer;import java.io.IOException;public class CharsetTransform{public static void main(String []args) throws IOException{Charset cn = Charset.forName("GBK");CharsetEncoder cnEncoder = cn.newEncoder();CharsetDecoder cnDecoder = cn.newDecoder();CharBuffer charBuffer = CharBuffer.allocate(8);charBuffer.put('郑');charBuffer.put('王');charBuffer.put('李');charBuffer.flip();System.out.println("charBuffer = "+charBuffer);System.out.println("capacity = "+charBuffer.capacity());System.out.println("position = "+charBuffer.position());System.out.println("limit = "+charBuffer.limit());ByteBuffer byteBuffer = cnEncoder.encode(charBuffer);System.out.println("===================encode====================");//encode 之后System.out.println("charBuffer = "+charBuffer);System.out.println("capacity = "+charBuffer.capacity());System.out.println("position = "+charBuffer.position());System.out.println("limit = "+charBuffer.limit());for(int i=0;i<byteBuffer.capacity();i++){System.out.println(byteBuffer.get(i));}System.out.println("byteBuffer = "+cnDecoder.decode(byteBuffer));System.out.println("charBuffer = "+charBuffer.get(1));}}charBuffer = 郑王李capacity = 8position = 0limit = 3===================encode====================charBuffer =capacity = 8position = 3limit = 3-42-93-51-11-64-18byteBuffer = 郑王李charBuffer = 王

0 0