Use this class to detect the encoding of an HTML page in a response stream. The class first checks if the encoding is defined in the
content type header of the response. If the encoding is not there, it parses the HTML to find the CHARSET tag and read the encoding.
If no encoding was detected, UTF-8 is assumed.
After the detectEncoding was called, the calling process must replace its response stream with the getInputStream(). The detected
encoding is returned by the getEncoding()
Example for using the class for reading an InputStream of an HTML to a String:
EncodingDetector detector = new EncodingDetector();
detector.detectEncoding ( contentTypeHeader, responseStream);
responseStream = detector.getInputStream();
String encoding = detector.getEncoding();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(responseStream, encoding);
StringBuffer htmlPage = new StringBuffer();
String line = null;
line = bufferedReader.readLine();
while (line != null) {
htmlPage.append(line);
line = bufferedReader.readLine();
if(line!=null)
htmlPage.append("\n");
}
At the end of this code the htmlPage contains a String representation with the HTML from the responseStream readable in the right encoding