Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having this functionality in a stream could be useful #19

Open
GoogleCodeExporter opened this issue Jan 25, 2016 · 1 comment
Open

Having this functionality in a stream could be useful #19

GoogleCodeExporter opened this issue Jan 25, 2016 · 1 comment

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 23 Jul 2013 at 3:32

@GoogleCodeExporter
Copy link
Author

the line:
   detector.handleData(b, 0, nrOfBytesRead);

should probably be:
   detector.handleData(b, off, nrOfBytesRead);

Original comment by [email protected] on 24 Jul 2013 at 5:07

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant