Here you can find the source of writeFully(FileChannel channel, long offset, ByteBuffer buf)
static void writeFully(FileChannel channel, long offset, ByteBuffer buf) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import java.nio.*; import java.nio.channels.FileChannel; import java.nio.file.StandardOpenOption; public class Main { static void writeFully(FileChannel channel, long offset, ByteBuffer buf) throws IOException { int remaining = buf.limit() - buf.position(); while (remaining > 0) { int written = channel.write(buf, offset); if (written < 0) throw new EOFException(); remaining -= written;//from w w w. j a v a 2s .c o m } } static void writeFully(FileChannel channel, long offset, File from) throws IOException { long fromLen = from.length(); FileChannel fromc = FileChannel.open(from.toPath(), StandardOpenOption.READ); long remaining = fromLen; while (remaining > 0) { long written = channel.transferFrom(fromc, offset + fromLen - remaining, remaining); if (written < 0) throw new EOFException(); remaining -= written; } fromc.close(); } }