Here you can find the source of readChars(ByteBuffer bb, int length)
public static String readChars(ByteBuffer bb, int length) throws IOException
//package com.java2s; //License from project: LGPL import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.SeekableByteChannel; public class Main { private static final String CHARSET_ISO8859 = "ISO-8859-1"; public static String readChars(SeekableByteChannel channel, int length) throws IOException { ByteBuffer bb = ByteBuffer.allocate(length); bb.order(ByteOrder.LITTLE_ENDIAN); channel.read(bb);// www. j a va 2 s. c o m bb.rewind(); return readChars(bb, bb.remaining()); } public static String readChars(ByteBuffer bb, int length) throws IOException { byte[] b = new byte[length]; bb.get(b); // lookup null character for string termination int strLength = 0; for (int i = 0; i < b.length; i++) { if (b[i] == 0) { break; } strLength++; } return new String(b, 0, strLength, CHARSET_ISO8859); } }