Here you can find the source of read(ReadableByteChannel channel, ByteBuffer buffer)
public static int read(ReadableByteChannel channel, ByteBuffer buffer) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; public class Main { public static int read(ReadableByteChannel channel, ByteBuffer buffer) throws IOException { int rem = buffer.position(); while (channel.read(buffer) != -1 && buffer.hasRemaining()) { }//from ww w . ja v a 2 s. c om return buffer.position() - rem; } }