private boolean found = false;
public FileCharsetDetector( File detectedFile )
throws FileNotFoundException, IOException
{
nsDetector det = new nsDetector( nsPSMDetector.ALL );
det.Init( new nsICharsetDetectionObserver()
{
public void Notify( String charset )
{
FileCharsetDetector.this.charset = charset;
FileCharsetDetector.this.found = true;
}
} );
FileInputStream fileInputStream = new FileInputStream( detectedFile );
BufferedInputStream imp = new BufferedInputStream( fileInputStream );
try
{
byte[] buf = new byte[1024];
int len;
boolean done = false;
boolean isAscii = true;
while ( ( len = imp.read( buf, 0, buf.length ) ) != -1 )
{
// Check if the stream is only ascii.
if ( isAscii )
{
isAscii = det.isAscii( buf, len );
}
// DoIt if non-ascii and not done yet.
if ( !isAscii && !done )
{
done = det.DoIt( buf, len, false );
found = done;
}
}
det.DataEnd();
if ( !isFound() )
{
String[] prob = det.getProbableCharsets();
if ( prob.length > 0 )
{
charset = prob[0];
}