} else if (hasUtf1BOM(buf, len)) {
charset = UTF1;
buffered.write(buf, 3, len - 3);
} else {
// Use jchardet which tries a variety of heuristics to choose an encoding.
nsDetector det = new nsDetector(nsPSMDetector.ALL);
class Observer implements nsICharsetDetectionObserver {
String charset;
public void Notify(String charset) {
this.charset = charset;
}
}
// The below is adapted from the main method in HtmlCharsetDetector.
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(