static int readAllBytes ( InputStream in , byte [ ] buffer ) throws IOException { int index = 0 ; while ( index < buffer . length ) { int read = in . read ( buffer , index , buffer . length - index ) ; if ( read == - 1 ) { return index ; } index += read ; } return index ; }
Attempts to fill the buffer by reading as many bytes as available . The returned number indicates how many bytes were read which may be smaller than the buffer size if EOF was reached .