Here you can find the source of writeRemaining(WritableByteChannel channel, ByteBuffer buffer)
public static void writeRemaining(WritableByteChannel 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.FileChannel; import java.nio.channels.WritableByteChannel; public class Main { private final static int MAX_NOOP_TRIALS = 1024; public static void writeRemaining(WritableByteChannel channel, ByteBuffer buffer) throws IOException { int noopCountDown = MAX_NOOP_TRIALS; while (buffer.hasRemaining()) { if (channel.write(buffer) == 0) { if (--noopCountDown == 0) throw new IOException("Write failed after " + MAX_NOOP_TRIALS + " trials"); continue; }// w w w. j a va 2 s . c o m noopCountDown = MAX_NOOP_TRIALS; } } public static void writeRemaining(FileChannel file, long position, ByteBuffer buffer) throws IOException { int noopCountDown = MAX_NOOP_TRIALS; while (buffer.hasRemaining()) { int amountWritten = file.write(buffer, position); position += amountWritten; if (amountWritten == 0) { if (--noopCountDown == 0) throw new IOException("Write failed after " + MAX_NOOP_TRIALS + " trials"); continue; } noopCountDown = MAX_NOOP_TRIALS; } } }