Module org.htmlunit

Class EncodingSniffer

java.lang.Object
org.htmlunit.util.EncodingSniffer

public final class EncodingSniffer extends Object
Sniffs encoding settings from HTML, XML or other content. The HTML encoding sniffing algorithm is based on the HTML5 encoding sniffing algorithm.
Author:
Daniel Gredler, Ahmed Ashour, Ronald Brill, Lai Quang Duong
  • Method Details

    • sniffEncodingFromMetaTag

      public static Charset sniffEncodingFromMetaTag(InputStream is) throws IOException
      Attempts to sniff an encoding from an HTML meta tag in the specified input stream.
      Parameters:
      is - the content stream to check for an HTML meta tag
      Returns:
      the encoding sniffed from the stream, or null if it could not be determined
      Throws:
      IOException - if an I/O error occurs
    • extractEncodingFromContentType

      public static Charset extractEncodingFromContentType(String s)
      Extracts an encoding from the specified Content-Type value using the IETF algorithm; if no encoding is found, returns null.
      Parameters:
      s - the Content-Type value to search for an encoding
      Returns:
      the encoding found in the specified value, or null if none was found
    • sniffEncodingFromXmlDeclaration

      public static Charset sniffEncodingFromXmlDeclaration(InputStream is) throws IOException
      Searches the specified XML content for an XML declaration and returns the encoding if found, otherwise returns null.
      Parameters:
      is - the content stream to check for a charset declaration
      Returns:
      the encoding of the specified XML content, or null if it could not be determined
      Throws:
      IOException - if an I/O error occurs
    • sniffEncodingFromCssDeclaration

      public static Charset sniffEncodingFromCssDeclaration(InputStream is) throws IOException
      Parses and returns the charset declaration at the start of a CSS file if present, otherwise returns null. e.g. @charset "UTF-8"
      Parameters:
      is - the input stream to parse
      Returns:
      the charset found at the start of the CSS file, or null if none
      Throws:
      IOException - if an I/O error occurs
    • toCharset

      public static Charset toCharset(String charsetName)
      Returns the Charset for the specified charset name if it is supported on this platform, or null if it is not.
      Parameters:
      charsetName - the charset name to look up
      Returns:
      the Charset for the given name, or null if unsupported
    • translateEncodingLabel

      public static String translateEncodingLabel(String encodingLabel)
      Translates the given encoding label into a normalized form according to the WHATWG Encoding specification.
      Parameters:
      encodingLabel - the label to translate
      Returns:
      the normalized encoding name, or null if not found