Created
April 3, 2014 22:38
-
-
Save veysiertekin/9964277 to your computer and use it in GitHub Desktop.
Determine encoding from byte array or any input stream.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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