private void copyTo(ByteBuffer buf,OutputStream out) throws IOException {
  if (!buf.hasRemaining()) {
    return;
  }
  if (buf.hasArray()) {
    out.write(buf.array(),buf.arrayOffset() + buf.position(),buf.remaining());
  }
 else {
    byte[] bytes=new byte[4096];
    do {
      buf.get(bytes,0,Math.min(bytes.length,buf.remaining()));
      out.write(bytes);
    }
 while (buf.hasRemaining());
  }
}

private void copyTo ( ByteBuffer buf , OutputStream out ) throws IOException { if ( ! buf . hasRemaining ( ) ) { return ; } if ( buf . hasArray ( ) ) { out . write ( buf . array ( ) , buf . arrayOffset ( ) + buf . position ( ) , buf . remaining ( ) ) ; } else { byte [ ] bytes = new byte [ 4096 ] ; do { buf . get ( bytes , 0 , Math . min ( bytes . length , buf . remaining ( ) ) ) ; out . write ( bytes ) ; } while ( buf . hasRemaining ( ) ) ; } }
copy the contents of a bytebuffer to an outputstream .
