We store some metadata for the stream contents (like hashes), and we wanted to
determine the encoding with it as well. I have therefore wrapped the
UniversalDetector inside a stream to be able to do several actions in one step
using nested streams.
Maybe it is useful to others:
public class EncodingDetectorInputStream extends BufferedInputStream {
private final UniversalDetector detector = new UniversalDetector(null);
public EncodingDetectorInputStream(InputStream in) {
super(in);
}
public String getDetectedCharset() {
return detector.getDetectedCharset();
}
@Override
public synchronized int read(byte[] b, int off, int len) throws IOException {
final int nrOfBytesRead = super.read(b, off, len);
if (!detector.isDone() && nrOfBytesRead > 0) {
detector.handleData(b, 0, nrOfBytesRead);
}
if (nrOfBytesRead == -1) {
detector.dataEnd();
}
return nrOfBytesRead;
}
}
Original issue reported on code.google.com by guy.mah...@gmail.com on 23 Jul 2013 at 3:32
Original issue reported on code.google.com by
guy.mah...@gmail.com
on 23 Jul 2013 at 3:32