private static String getBOMEncoding ( final BufferedInputStream is ) throws IOException { String encoding = null ; final int [ ] bytes = new int [ 3 ] ; is . mark ( 3 ) ; bytes [ 0 ] = is . read ( ) ; bytes [ 1 ] = is . read ( ) ; bytes [ 2 ] = is . read ( ) ; if ( bytes [ 0 ] == 0xFE && bytes [ 1 ] == 0xFF ) { encoding = UTF_16BE ; is . reset ( ) ; is . read ( ) ; is . read ( ) ; } else if ( bytes [ 0 ] == 0xFF && bytes [ 1 ] == 0xFE ) { encoding = UTF_16LE ; is . reset ( ) ; is . read ( ) ; is . read ( ) ; } else if ( bytes [ 0 ] == 0xEF && bytes [ 1 ] == 0xBB && bytes [ 2 ] == 0xBF ) { encoding = UTF_8 ; } else { is . reset ( ) ; } return encoding ; }
if there was BOM the in the stream it is consumed