public static byte [ ] read ( InputStream in , boolean closeAfterwards ) throws IOException { byte [ ] buffer = new byte [ 32 * 1024 ] ; ByteArrayOutputStream bas = new ByteArrayOutputStream ( ) ; for ( int length ; ( length = in . read ( buffer , 0 , buffer . length ) ) > - 1 ; bas . write ( buffer , 0 , length ) ) ; if ( closeAfterwards ) in . close ( ) ; return bas . toByteArray ( ) ; }
Reads the whole contents of an input stream into a byte array closing the stream if so requested .