@ Trivial private static byte [ ] read ( InputStream inputStream , int expectedRead , String name ) throws IOException { byte [ ] bytes = new byte [ expectedRead ] ; int remainingRead = expectedRead ; int totalRead = 0 ; while ( remainingRead > 0 ) { int nextRead = inputStream . read ( bytes , totalRead , remainingRead ) ; // throws IOException if ( nextRead <= 0 ) { // 'nextRead == 0' should only ever happen if 'remainingRead == 0', which ought // never be the case here. Treat a '0' return value as an error. // // 'nextRead == -1' means the end of input was reached. throw new IOException ( "Read only [ " + Integer . valueOf ( totalRead ) + " ]" + " of expected [ " + Integer . valueOf ( expectedRead ) + " ] bytes" + " from [ " + name + " ]" ) ; } else { remainingRead -= nextRead ; totalRead += nextRead ; } } return bytes ; }
Read an exact count of bytes from an input stream .