Here you can find the source of read(ReadableByteChannel channel, int amount, ByteBuffer dest)
public static int read(ReadableByteChannel channel, int amount, ByteBuffer dest) throws IOException
//package com.java2s; import java.io.IOException; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; public class Main { public static int read(ReadableByteChannel channel, int amount, ByteBuffer dest) throws IOException { int read = 0; int last = 0; if (dest.remaining() < amount) { throw new BufferOverflowException(); }/*ww w. java 2 s . com*/ while (read < amount && last != -1) { last = channel.read(dest); if (last != -1) read += last; } return (read == 0 && last == -1) ? -1 : read; } }