private static boolean isBinary ( InputStream in ) { try { int size = in . available ( ) ; if ( size > 1024 ) size = 1024 ; byte [ ] data = new byte [ size ] ; in . read ( data ) ; in . close ( ) ; int ascii = 0 ; int other = 0 ; for ( int i = 0 ; i < data . length ; i ++ ) { byte b = data [ i ] ; if ( b < 0x09 ) return true ; if ( b == 0x09 || b == 0x0A || b == 0x0C || b == 0x0D ) ascii ++ ; else if ( b >= 0x20 && b <= 0x7E ) ascii ++ ; else other ++ ; } return other != 0 && 100 * other / ( ascii + other ) > 95 ; } catch ( IOException e ) { throw E . ioException ( e ) ; } }
Guess whether given file is binary . Just checks for anything under 0x09 .