com.sapportals.portal.ivs.http

Class EncodingDetector

java.lang.Object
  extended by com.sapportals.portal.ivs.http.EncodingDetector

public class EncodingDetector
extends Object

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


Constructor Summary
EncodingDetector()
           
 
Method Summary
 boolean detectEncoding(String contentTypeHeader, InputStream responseStream)
          Tries to detect the encoding in the header or the response stream passed to it.
 String getEncoding()
           
 InputStream getInputStream()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EncodingDetector

public EncodingDetector()
Method Detail

detectEncoding

public boolean detectEncoding(String contentTypeHeader,
                              InputStream responseStream)
Tries to detect the encoding in the header or the response stream passed to it.

Parameters:
contentTypeHeader - extracted from the response headers
responseStream - the actual response stream returned from the site
Returns:
boolean indicates whether the encoding detection succeeded or not

getInputStream

public InputStream getInputStream()
Returns:
InputStream for the calling process to use instead of the one passed to the detector. The original input stream must be replaced

getEncoding

public String getEncoding()
Returns:
String encoding as was detected in the response stream or content type header
Access Rights

This class can be accessed from:


SC DC Public Part ACH
[sap.com] EP-RUNTIME [sap.com] tc/ep/connectivity/services/api api EP-PIN


Copyright 2011 SAP AG Complete Copyright Notice