public static void copy(ByteBuffer buffer,OutputStream out) throws IOException {
  if (buffer.hasArray()) {
    out.write(buffer.array(),buffer.arrayOffset() + buffer.position(),buffer.remaining());
  }
 else {
    byte[] arr=new byte[BUFFER_SIZE];
    ByteBuffer in=buffer.duplicate();
    while (in.hasRemaining()) {
      int len=Math.min(in.remaining(),arr.length);
      in.get(arr,0,len);
      out.write(arr,0,len);
    }
  }
}

public static void copy ( ByteBuffer buffer , OutputStream out ) throws IOException { if ( buffer . hasArray ( ) ) { out . write ( buffer . array ( ) , buffer . arrayOffset ( ) + buffer . position ( ) , buffer . remaining ( ) ) ; } else { byte [ ] arr = new byte [ BUFFER_SIZE ] ; ByteBuffer in = buffer . duplicate ( ) ; while ( in . hasRemaining ( ) ) { int len = Math . min ( in . remaining ( ) , arr . length ) ; in . get ( arr , 0 , len ) ; out . write ( arr , 0 , len ) ; } } }
copies all remaining bytes from the byte buffer to the output stream . does not modify the buffer or flush or close the output stream .
