FileInputStream input = new FileInputStream(file);
BufferedInputStream buffer = new BufferedInputStream(input);
byte[] data = new byte[buffer.available()];
int available = -1;
Detector det = new Detector(PSMDetector.ALL);
//Set an observer...
//The Notify() will be called when a matching charset is found.
det.init(new ICharsetDetectionObserver()
{
public void notify(String charset)
{
System.out.println("CHARSET === " + charset);
}
});
boolean done = false;
boolean isAscii = true;
while ((available = buffer.read(data)) > -1)
{
//Khoilv'code.
//System.out.print(data);
if (isAscii)
isAscii = det.isAscii(data, available);
//DoIt if non-ascii and not done yet.
if (!isAscii && !done)
done = det.doIt(data, available, false);
}
det.dataEnd();
if (isAscii)
{
System.out.println("CHARSET = ASCII");
found = true;
}
if (!found)
{
String prob[] = det.getProbableCharsets();
for (int i = 0; i < prob.length; i++)
{
System.out.println("Probable Charset = " + prob[i]);
}
}