Here you can find the source of writeFromBuffer(SocketChannel channel, ByteBuffer buf, int sleepMsecs)
static public int writeFromBuffer(SocketChannel channel, ByteBuffer buf, int sleepMsecs)
//package com.java2s; //License from project: BSD License import java.io.*; import java.nio.*; import java.nio.channels.*; public class Main { static public int writeFromBuffer(SocketChannel channel, ByteBuffer buf, int sleepMsecs) { if (channel == null || buf.limit() == 0) { return -1; }/*from w ww . j ava 2 s .com*/ int totWritten = 0; buf.rewind(); // write until capacity reached while (totWritten < buf.limit()) { int nWritten; try { nWritten = channel.write(buf); } catch (IOException e) { System.err.println("ERROR - NioUtils.writeFromBuffer"); System.err.println(" Writing buffer to channel"); return -1; } catch (BufferOverflowException e) { System.err.println("ERROR - NioUtils.writeFromBuffer"); System.err.println(" Buffer overflow"); return -1; } if (nWritten < 0) { return -1; } totWritten += nWritten; // sleep if stalled if (nWritten == sleepMsecs) { _sleep(10); } } // rewind buffer, in case it will be reused buf.rewind(); return 0; } static private void _sleep(int msecs) { Thread t = Thread.currentThread(); try { t.sleep(msecs); } catch (InterruptedException e) { } } }