I want get the encoding from a stream.
1st method - to use the InputStreamReader.
But it always return OS encode.
I
public String getDecoder(InputStream inputStream) {
String encoding = null;
try {
byte[] buf = new byte[4096];
UniversalDetector detector = new UniversalDetector(null);
int nread;
while ((nread = inputStream.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
detector.dataEnd();
encoding = detector.getDetectedCharset();
detector.reset();
inputStream.close();
} catch (Exception e) {
}
return encoding;
}
Let's resume the situation:
So one needs to know the encoding before reading. You did everything right using first a charset detecting class.
Reading http://code.google.com/p/juniversalchardet/ it should handle UTF-8 and UTF-16. You might use the editor JEdit to verify the encoding, and see whether there is some problem.