Here you can find the source of byteBufferToString(ByteBuffer buffer)
public static String byteBufferToString(ByteBuffer buffer)
//package com.java2s; //License from project: Apache License import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; public class Main { public static String byteBufferToString(ByteBuffer buffer) { String data = ""; CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); try {/*ww w . j a v a 2s . c o m*/ int old_position = buffer.position(); data = decoder.decode(buffer).toString(); buffer.position(old_position); } catch (Exception e) { e.printStackTrace(); return ""; } return data; } }