public static byte [ ] toByteArray ( final HttpEntity entity ) throws IOException { if ( entity == null ) { throw new IllegalArgumentException ( "HTTP entity may not be null" ) ; } InputStream instream = entity . getContent ( ) ; if ( instream == null ) { return null ; } try { if ( entity . getContentLength ( ) > Integer . MAX_VALUE ) { throw new IllegalArgumentException ( "HTTP entity too large to be buffered in memory" ) ; } int i = ( int ) entity . getContentLength ( ) ; if ( i < 0 ) { i = 4096 ; } ByteArrayBuffer buffer = new ByteArrayBuffer ( i ) ; byte [ ] tmp = new byte [ 4096 ] ; int l ; while ( ( l = instream . read ( tmp ) ) != - 1 ) { buffer . append ( tmp , 0 , l ) ; } return buffer . toByteArray ( ) ; } finally { instream . close ( ) ; } }
Read the contents of an entity and return it as a byte array .