public static Pair<Reader,String> guessCharset(InputStream in) throws IOException {
  ByteArrayOutputStream buffered=new ByteArrayOutputStream();
  byte[] buf=new byte[1024];
  boolean isAscii=true;
  int len=in.read(buf);
  if (len <= 0) {
    return Pair.pair((Reader)new StringReader(""),UTF8);
  }
  String charset=findCharset(buf,len);
  if (charset != null) {
    buffered.write(buf,0,len);
  }
 else   if (hasUtf8BOM(buf,len)) {
    charset=UTF8;
    buffered.write(buf,3,len - 3);
  }
 else   if (hasUtf32BEBOM(buf,len)) {
    charset=UTF32BE;
    buffered.write(buf,4,len - 4);
  }
 else   if (hasUtf32LEBOM(buf,len)) {
    charset=UTF32LE;
    buffered.write(buf,4,len - 4);
  }
 else   if (hasUtf16BEBOM(buf,len)) {
    charset=UTF16BE;
    buffered.write(buf,2,len - 2);
  }
 else   if (hasUtf16LEBOM(buf,len)) {
    charset=UTF16LE;
    buffered.write(buf,2,len - 2);
  }
 else   if (hasUtf7BOM(buf,len)) {
    charset=UTF7;
    buffered.write(buf,4,len - 4);
  }
 else   if (hasUtf1BOM(buf,len)) {
    charset=UTF1;
    buffered.write(buf,3,len - 3);
  }
 else {
    nsDetector det=new nsDetector(nsPSMDetector.ALL);
    Observer observer=new Observer();
    det.Init(observer);
    do {
      buffered.write(buf,0,len);
      if (isAscii) {
        isAscii=det.isAscii(buf,len);
      }
      if (!isAscii) {
        if (det.DoIt(buf,len,false)) {
          break;
        }
      }
    }
 while ((len=in.read(buf)) > 0);
    det.DataEnd();
    charset=observer.charset;
  }
  if (charset != null) {
    charset=supportedCharsetName(charset);
  }
  if (charset == null) {
    charset=UTF8;
  }
  return Pair.pair(joinStreamsWithCharset(buffered.toByteArray(),in,charset),charset);
}

public static Pair < Reader , String > guessCharset ( InputStream in ) throws IOException { ByteArrayOutputStream buffered = new ByteArrayOutputStream ( ) ; byte [ ] buf = new byte [ 1024 ] ; boolean isAscii = true ; int len = in . read ( buf ) ; if ( len <= 0 ) { return Pair . pair ( ( Reader ) new StringReader ( "" ) , UTF8 ) ; } String charset = findCharset ( buf , len ) ; if ( charset != null ) { buffered . write ( buf , 0 , len ) ; } else if ( hasUtf8BOM ( buf , len ) ) { charset = UTF8 ; buffered . write ( buf , 3 , len - 3 ) ; } else if ( hasUtf32BEBOM ( buf , len ) ) { charset = UTF32BE ; buffered . write ( buf , 4 , len - 4 ) ; } else if ( hasUtf32LEBOM ( buf , len ) ) { charset = UTF32LE ; buffered . write ( buf , 4 , len - 4 ) ; } else if ( hasUtf16BEBOM ( buf , len ) ) { charset = UTF16BE ; buffered . write ( buf , 2 , len - 2 ) ; } else if ( hasUtf16LEBOM ( buf , len ) ) { charset = UTF16LE ; buffered . write ( buf , 2 , len - 2 ) ; } else if ( hasUtf7BOM ( buf , len ) ) { charset = UTF7 ; buffered . write ( buf , 4 , len - 4 ) ; } else if ( hasUtf1BOM ( buf , len ) ) { charset = UTF1 ; buffered . write ( buf , 3 , len - 3 ) ; } else { nsDetector det = new nsDetector ( nsPSMDetector . ALL ) ; Observer observer = new Observer ( ) ; det . Init ( observer ) ; do { buffered . write ( buf , 0 , len ) ; if ( isAscii ) { isAscii = det . isAscii ( buf , len ) ; } if ( ! isAscii ) { if ( det . DoIt ( buf , len , false ) ) { break ; } } } while ( ( len = in . read ( buf ) ) > 0 ) ; det . DataEnd ( ) ; charset = observer . charset ; } if ( charset != null ) { charset = supportedCharsetName ( charset ) ; } if ( charset == null ) { charset = UTF8 ; } return Pair . pair ( joinStreamsWithCharset ( buffered . toByteArray ( ) , in , charset ) , charset ) ; }
given a byte stream , figure out an encoding and return a character stream and the encoding used to convert bytes to characters .
