private long countBySkipping ( InputStream in ) throws IOException { long count = 0 ; while ( true ) { // don't try to skip more than available() // things may work really wrong with FileInputStream otherwise long skipped = in . skip ( Math . min ( in . available ( ) , Integer . MAX_VALUE ) ) ; if ( skipped <= 0 ) { if ( in . read ( ) == - 1 ) { return count ; } else if ( count == 0 && in . available ( ) == 0 ) { // if available is still zero after reading a single byte, it // will probably always be zero, so we should countByReading throw new IOException ( ) ; } count ++ ; } else { count += skipped ; } } }
Counts the bytes in the given input stream using skip if possible . Returns SKIP_FAILED if the first call to skip threw in which case skip may just not be supported .