public static void writeTo(final HttpEntity httpEntity, final OutputStream outstream) throws IOException {
        Args.notNull(outstream, "Output stream");
        try (InputStream instream = httpEntity.getContent()) {
            final byte[] buffer = new byte[OUTPUT_BUFFER_SIZE];
            int l;
            if (httpEntity.getContentLength() < 0) {
                // consume until EOF
                while ((l = instream.read(buffer)) != -1) {
                    outstream.write(buffer, 0, l);
                    outstream.flush();
                    LOG.debug("Flushed {} bytes of data");
                }
            } else {
                // consume no more than length
                long remaining = httpEntity.getContentLength();
                while (remaining > 0) {
                    l = instream.read(buffer, 0, (int) Math.min(OUTPUT_BUFFER_SIZE, remaining));
                    if (l == -1) {
                        break;
                    }
                    outstream.write(buffer, 0, l);
                    outstream.flush();
                    LOG.debug("Flushed {} bytes of data");
                    remaining -= l;
                }
            }
        }
    }
public static void writeTo ( final HttpEntity httpEntity , final OutputStream outstream ) throws IOException { Args . notNull ( outstream , "Output stream" ) ; try ( InputStream instream = httpEntity . getContent ( ) ) { final byte [ ] buffer = new byte [ OUTPUT_BUFFER_SIZE ] ; int l ; if ( httpEntity . getContentLength ( ) < 0 ) { while ( ( l = instream . read ( buffer ) ) != - 1 ) { outstream . write ( buffer , 0 , l ) ; outstream . flush ( ) ; LOG . debug ( "Flushed {} bytes of data" ) ; } } else { long remaining = httpEntity . getContentLength ( ) ; while ( remaining > 0 ) { l = instream . read ( buffer , 0 , ( int ) Math . min ( OUTPUT_BUFFER_SIZE , remaining ) ) ; if ( l == - 1 ) { break ; } outstream . write ( buffer , 0 , l ) ; outstream . flush ( ) ; LOG . debug ( "Flushed {} bytes of data" ) ; remaining -= l ; } } } }
Copied from org.apache.http.entity.InputStreamEntity.writeTo(OutputStream) method but flushes the buffer after
each read in order to allow streaming and web sockets.

@param httpEntity
The entity to copy to the OutputStream
@param outstream
The OutputStream
@throws IOException
If a problem occurs