ByteBuffer 转化为 String

1 public  String decode(ByteBuffer buffer)
 2 {
 3         System.out.println( " buffer= "   +  buffer);
 4         Charset charset  =   null ;
 5         CharsetDecoder decoder  =   null ;
 6         CharBuffer charBuffer  =   null ;
 7          try
 8      {
 9             charset  =  Charset.forName( " gb2312 " );
10             decoder  =  charset.newDecoder();
11             charBuffer  =  decoder.decode(buffer);
12             System.out.println( " charBuffer= "   +  charBuffer);
13             System.out.println(charBuffer.toString());
14              return  charBuffer.toString();
15         }

16          catch  (Exception ex)
17      {
18             ex.printStackTrace();
19              return   "" ;
20         }

21  }

你可能感兴趣的:(Java,string,buffer,null)