Here you can find the source of toString(ByteBuffer buffer, String encoding)
public static String toString(ByteBuffer buffer, String encoding)
//package com.java2s; //License from project: Open Source License import java.io.*; import java.nio.ByteBuffer; public class Main { public static String toString(ByteBuffer buffer, String encoding) { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes);/* ww w . j a v a 2 s . c om*/ return fromBytes(bytes, encoding); } public static String fromBytes(byte[] b) { return fromBytes(b, "UTF-8"); } public static String fromBytes(byte[] b, String encoding) { try { return new String(b, encoding); } catch (UnsupportedEncodingException e) { return new String(b); } } }