Skip to content

Instantly share code, notes, and snippets.

@veysiertekin
Created April 3, 2014 22:38
Show Gist options
  • Save veysiertekin/9964277 to your computer and use it in GitHub Desktop.
Save veysiertekin/9964277 to your computer and use it in GitHub Desktop.
Determine encoding from byte array or any input stream.
/*
* Download "UniversalDetector" from here: https://code.google.com/p/juniversalchardet/
*/
public class FindEncoding {
private FindEncoding(){}
public static String findEncoding(byte[] bytes) {
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(bytes, 0, bytes.length);
detector.dataEnd();
return detector.getDetectedCharset();
}
public static String findEncoding(InputStream stream) throws IOException {
byte[] buf = new byte[4096];
int nread;
UniversalDetector detector = new UniversalDetector(null);
while ((nread = stream.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
detector.dataEnd();
return detector.getDetectedCharset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment