private static void writeAndFlush ( final ByteBuffer buffer , final OutputStream outputStream ) throws IOException { if ( buffer . hasArray ( ) ) { outputStream . write ( buffer . array ( ) , buffer . position ( ) , buffer . remaining ( ) ) ; } else { // cannot access underlying byte array, need to copy into a temporary array while ( buffer . hasRemaining ( ) ) { // figure out how much to read, but use an upper limit of 8kb. LogMessages should be rather // small so we don't expect this to get hit but avoid large temporary buffers, just in case. final int size = Math . min ( buffer . remaining ( ) , 8 * 1024 ) ; final byte [ ] chunk = new byte [ size ] ; buffer . get ( chunk ) ; outputStream . write ( chunk ) ; } } outputStream . flush ( ) ; }
Write the contents of the given ByteBuffer to the OutputStream and flush the stream .