Here you can find the source of redString(ByteBuffer buffer, int offset, Charset charset)
public static String redString(ByteBuffer buffer, int offset, Charset charset) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; public class Main { public static String redString(ByteBuffer buffer, int offset, Charset charset) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); for (int i = offset; i < buffer.limit(); i++) { if (((char) buffer.get(i)) == '\0') { break; }//from w w w . j a v a2 s . c om out.write(new byte[] { buffer.get(i) }); } return new String(out.toByteArray(), charset); } }