Here you can find the source of writeFully(ByteBuffer buf, WritableByteChannel out)
public static void writeFully(ByteBuffer buf, WritableByteChannel out) throws Exception
//package com.java2s; //License from project: LGPL import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; public class Main { /**// w ww . ja v a 2 s.co m * if we were to register for OP_WRITE and send the remaining data on * readyOps for this channel we have to either block the caller thread or * queue the message buffers that may arrive while waiting for OP_WRITE. * Instead of the above approach this method will continuously write to the * channel until the buffer sent fully. */ public static void writeFully(ByteBuffer buf, WritableByteChannel out) throws Exception { int written = 0; int toWrite = buf.limit(); while (written < toWrite) { written += out.write(buf); } } }