Class HtmlCharset

java.lang.Object
com.vaadin.swingbridge.surrogates.internal.HtmlCharset

public final class HtmlCharset extends Object
Works out which charset a fetched HTML page is encoded in.

Order of evidence, matching what browsers do and what JEditorPane.setPage is expected to honour: the charset parameter of the Content-Type response header, then a <meta> declaration in the markup, then UTF-8.

The header parameter is deliberately not read from URLConnection.getContentEncoding() — that returns the Content-Encoding header, which carries the compression (gzip, deflate) and is unrelated to the charset. It is null on an ordinary response even when Content-Type declares a charset, so using it means silently decoding every non-UTF-8 page as UTF-8.

  • Method Details

    • detect

      public static Charset detect(String contentTypeHeader, byte[] body)
      Parameters:
      contentTypeHeader - the Content-Type response header, or null
      body - the fetched bytes; only the head is inspected
      Returns:
      the charset to decode with; never null
    • charsetParam

      public static String charsetParam(String contentTypeHeader)
      The charset parameter of a Content-Type header, or null.
    • isHtmlMediaType

      public static boolean isHtmlMediaType(String contentTypeHeader)
      Whether the header's media type is HTML. Used to WARN when a page is served as something else, since the pane renders it as HTML regardless.