Class CharsetDetector

java.lang.Object
com.norconex.importer.charset.CharsetDetector

public class CharsetDetector extends Object
Character set detector.
  • Constructor Details

  • Method Details

    • builder

      public static CharsetDetector.CharsetDetectorBuilder builder()
    • detect

      public Charset detect(Doc doc) throws IOException

      Detects a document character using the following logic, in order:

      • If the document is null, returns the fallback charset (which is UTF-8 unless explicitly set to something different).
      • If priority charset supplier is not null and returns a valid charset, returns that charset.
      • If the charset has already been detected for the document, returns that charset (Doc.getCharset()).
      • Try to detect the character set, using any declared charset to influence the outcome.
      • If it can't be detected, returns the declared charset.
      • If no declared charset, returns the fallback charset (UTF-8 by default).

      This method will NOT set the detected encoding on the DocHandlerContext.

      Parameters:
      doc - document to detect encoding on when applicable
      Returns:
      the detected charset (never null).
      Throws:
      IOException - problem detecting charset
    • detect

      public Charset detect(String input)

      Detects the character encoding of a string.

      Detection occurs using the following logic, in order:

      • If the string is null, returns the fallback charset (which is UTF-8 unless explicitly set to something different).
      • If priority charset supplier is not null and returns a valid charset, returns that charset.
      • Try to detect the character set, using any declared charset to influence the outcome.
      • If it can't be detected, returns the declared charset.
      • If no declared charset, returns the fallback charset (UTF-8 by default).
      Parameters:
      input - the input stream to detect encoding on
      Returns:
      the detected charset (never null).
    • detect

      public Charset detect(InputStream input) throws IOException

      Detects the character encoding of an input stream. InputStream.markSupported() must return true otherwise no decoding will be attempted and the fallback charset will be returned.

      Detection occurs using the following logic, in order:

      • If the stream is null or does not support marking, returns the fallback charset (which is UTF-8 unless explicitly set to something different).
      • If priority charset supplier is not null and returns a valid charset, returns that charset.
      • Try to detect the character set, using any declared charset to influence the outcome.
      • If it can't be detected, returns the declared charset.
      • If no declared charset, returns the fallback charset (UTF-8 by default).
      Parameters:
      input - the input stream to detect encoding on
      Returns:
      the detected charset (never null).
      Throws:
      IOException - problem detecting charset