Class EncodingDetector

java.lang.Object
org.omegat.util.EncodingDetector

public final class EncodingDetector extends Object
  • Method Details

    • detectEncoding

      public static String detectEncoding(File inFile) throws IOException
      Detect the encoding of the supplied file. Convenience method for detectEncoding(java.io.InputStream).
      Throws:
      IOException
    • detectEncoding

      public static String detectEncoding(InputStream stream) throws IOException
      Detect the encoding of the supplied file. The caller is responsible for closing the stream.
      Throws:
      IOException
      See Also:
    • detectEncodingDefault

      public static String detectEncodingDefault(File inFile, String defaultEncoding)
      Detect the encoding of the supplied file. If detection fails, return the supplied default encoding.
    • detectHtmlEncoding

      public static Charset detectHtmlEncoding(String fileName, String defaultEncoding)
      Returns the reader of the underlying file in the correct encoding.

      We can detect the following:

      • UTF-16 with BOM (byte order mark)
      • UTF-8 with BOM (byte order mark)
      • Any other encoding with 8-bit Latin symbols (e.g. Windows-1251, UTF-8 etc), if it is specified using XML/HTML-style encoding declarations.

      Note that we cannot detect UTF-16 encoding, if there's no BOM!