Here you can find the source of readIntoBuffer(SocketChannel channel, ByteBuffer buf, int sleepMsecs)
static public int readIntoBuffer(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 readIntoBuffer(SocketChannel channel, ByteBuffer buf, int sleepMsecs) { if (channel == null) { return -1; }//from w ww . ja v a 2s .c om buf.clear(); int totRead = 0; // read until capacity reached while (totRead < buf.capacity()) { int nRead; try { nRead = channel.read(buf); } catch (IOException e) { System.err.println("Error reading channel into buffer"); e.printStackTrace(); return -1; } if (nRead < 0) { return -1; } totRead += nRead; // sleep if no data available if (nRead == 0) { _sleep(sleepMsecs); } } // while // prepare buffer for use buf.rewind(); return 0; } static private void _sleep(int msecs) { Thread t = Thread.currentThread(); try { t.sleep(msecs); } catch (InterruptedException e) { } } }