Here you can find the source of getString(ByteBuffer buf, Charset encoding)
public static String getString(ByteBuffer buf, Charset encoding)
//package com.java2s; //License from project: Apache License import java.nio.ByteBuffer; import java.nio.charset.Charset; public class Main { /**// w w w . j a v a 2s. c o m * Converts all remaining bytes in the buffer a String using the specified encoding. * Does not move the buffer position. */ public static String getString(ByteBuffer buf, Charset encoding) { return getString(buf, 0, buf.remaining(), encoding); } /** * Converts the specified number of bytes in the buffer and converts them to a String using * the specified encoding. Does not move the buffer position. */ public static String getString(ByteBuffer buf, int offset, int length, Charset encoding) { buf = buf.duplicate(); buf.position(buf.position() + offset); if (buf.hasArray()) { return new String(buf.array(), buf.arrayOffset() + buf.position(), length, encoding); } else { byte[] bytes = new byte[length]; buf.get(bytes); return new String(bytes, encoding); } } }