public static void skipFully ( @ Nonnull final InputStream aIS , @ Nonnegative final long nBytesToSkip ) throws IOException { ValueEnforcer . notNull ( aIS , "InputStream" ) ; ValueEnforcer . isGE0 ( nBytesToSkip , "BytesToSkip" ) ; long nRemaining = nBytesToSkip ; while ( nRemaining > 0 ) { // May only return a partial skip final long nSkipped = aIS . skip ( nRemaining ) ; if ( nSkipped == 0 ) { // Check if we're at the end of the file or not // -> blocking read! if ( aIS . read ( ) == - 1 ) { throw new EOFException ( "Failed to skip a total of " + nBytesToSkip + " bytes on input stream. Only skipped " + ( nBytesToSkip - nRemaining ) + " bytes so far!" ) ; } nRemaining -- ; } else { // Skipped at least one char nRemaining -= nSkipped ; } } }
Fully skip the passed amounts in the input stream . Only forward skipping is possible!