* occurs when the detection is failed.
*/
public static String detectEncoding(byte[] data, String defaultEncoding) throws IOException {
CharsetDetector detector = new CharsetDetector();
detector.setText(data);
CharsetMatch cm = detector.detect();
String estimatedEncoding = cm.getName();
boolean isReliable = Charset.isSupported(estimatedEncoding) && cm.getConfidence() >= MINIMAL_CONFIDENCE_LEVEL;
return isReliable ? estimatedEncoding : defaultEncoding;
}
}