code: public static ByteBuffer allocateAndReadAll(int size, ReadableByteChannel channel) throws IOException
  	{
  		ByteBuffer buf = ByteBuffer.allocate(size);
  		int justRead;
  		int totalRead = 0;
  
  		// FIXME, this will be a tight loop if the channel is non-blocking...
  		while(totalRead < size)
  		{
  			logger.debug("reading totalRead={}", totalRead);
  			if((justRead = channel.read(buf))<0)
  				throw new EOFException("Unexpected end of stream after reading " + totalRead + " bytes");
  			totalRead += justRead;
  		}
  		buf.rewind();
  		return buf;
  	}
docs: Allocate a buffer and fill it from a channel. The returned
  buffer will be rewound to the begining.
  @return Buffer containing size bytes from the channel.
  @throws IOException if the channel read does.
  @throws EOFException if a end of stream is encountered
  before the full size is read.