net.sf.okapi.common

## Class BOMNewlineEncodingDetector

- java.lang.Object
- - net.sf.okapi.common.BOMNewlineEncodingDetector

-     public final class BOMNewlineEncodingDetector
      extends java.lang.Object

  Helper class to detect byte-order-mark and other easily guessed of encodings, as well as the type of line-break used in a given input. Based on information in: http://www.w3.org/TR/REC-xml/#sec-guessing-no-ext-info http://www.w3.org/TR/html401/charset.html#h-5.2

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `BOMNewlineEncodingDetector.NewlineType` Defines type friendly newline types.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static java.lang.String` | `BOCU_1` BOCU (Binary Ordered Compression for Unicode)
    `static java.lang.String` | `EBCDIC` Java friendly EBCDIC encoding name..
    `static java.lang.String` | `ISO_8859_1` Java friendly ISO-8859-1 encoding name.
    `static java.lang.String` | `SCSU` SCSU (Standard Compression Scheme for Unicode)
    `static java.lang.String` | `UTF_16` Java friendly UTF-16 encoding name.
    `static java.lang.String` | `UTF_16BE` Java friendly UTF-16 big endian encoding name.
    `static java.lang.String` | `UTF_16LE` Java friendly UTF-16 little endian encoding name.
    `static java.lang.String` | `UTF_32` Java friendly UTF-32 encoding name..
    `static java.lang.String` | `UTF_32BE` Java friendly UTF-32 big endian encoding name..
    `static java.lang.String` | `UTF_32LE` Java friendly UTF-32 little endian encoding name..
    `static java.lang.String` | `UTF_7` Java friendly UTF-7 encoding name..
    `static java.lang.String` | `UTF_8` Java friendly UTF-8 encoding name.
    `static java.lang.String` | `UTF_EBCDIC` Java friendly UTF-EBCDIC encoding name..

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BOMNewlineEncodingDetector(java.io.InputStream inputStream)` Create a new BOMNewlineEncodingDetector from an `InputStream`.
    `BOMNewlineEncodingDetector(java.io.InputStream inputStream, java.nio.charset.Charset defaultEncoding)`
    `BOMNewlineEncodingDetector(java.io.InputStream inputStream, java.lang.String defaultEncoding)` Create a new BOMNewlineEncodingDetector from an `InputStream` and a user provided encoding.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `detectAndRemoveBom()`
    `void` | `detectBom()`
    `int` | `getBomSize()` Gets the number of bytes used by the Byte-Order-mark in this document.
    `java.lang.String` | `getDefaultEncoding()` Get the defaultEncoding set by the user.
    `java.lang.String` | `getEncoding()` Get the guessed encoding or if encoding couldn't be guessed return the user supplied encoding.
    `java.lang.String` | `getEncodingSpecificationInfo()` Return a short description of the encoding.
    `java.io.InputStream` | `getInputStream()` Get the input stream pased in to the constructor
    `BOMNewlineEncodingDetector.NewlineType` | `getNewlineType()` Detects newline type using the inputStream itself.
    `static BOMNewlineEncodingDetector.NewlineType` | `getNewlineType(java.lang.CharSequence text)` Static helper method for detecting newline type used in a run of text.
    `boolean` | `hasBom()` Does this document have a byte order mark?
    `boolean` | `hasUtf7Bom()` Does this document have a UTF-7 byte order mark?
    `boolean` | `hasUtf8Bom()` Indicates if the guessed encoding is UTF-8 and this file has a BOM.
    `boolean` | `hasUtf8Encoding()`
    `boolean` | `isAutodetected()` Indicates if the guessed encoding was auto-detected.
    `boolean` | `isDefinitive()` Are we confident of the document encoding?
    `void` | `setDefaultEncoding(java.lang.String defaultEncoding)` Set the default encoding.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### UTF_16

          public static final java.lang.String UTF_16

      Java friendly UTF-16 encoding name.
      See Also:
        Constant Field Values

    - #### UTF_16BE

          public static final java.lang.String UTF_16BE

      Java friendly UTF-16 big endian encoding name.
      See Also:
        Constant Field Values

    - #### UTF_16LE

          public static final java.lang.String UTF_16LE

      Java friendly UTF-16 little endian encoding name.
      See Also:
        Constant Field Values

    - #### UTF_8

          public static final java.lang.String UTF_8

      Java friendly UTF-8 encoding name.
      See Also:
        Constant Field Values

    - #### ISO_8859_1

          public static final java.lang.String ISO_8859_1

      Java friendly ISO-8859-1 encoding name.
      See Also:
        Constant Field Values

    - #### EBCDIC

          public static final java.lang.String EBCDIC

      Java friendly EBCDIC encoding name..
      See Also:
        Constant Field Values

    - #### SCSU

          public static final java.lang.String SCSU

      SCSU (Standard Compression Scheme for Unicode)
      See Also:
        Constant Field Values

    - #### UTF_7

          public static final java.lang.String UTF_7

      Java friendly UTF-7 encoding name..
      See Also:
        Constant Field Values

    - #### UTF_EBCDIC

          public static final java.lang.String UTF_EBCDIC

      Java friendly UTF-EBCDIC encoding name..
      See Also:
        Constant Field Values

    - #### BOCU_1

          public static final java.lang.String BOCU_1

      BOCU (Binary Ordered Compression for Unicode)
      See Also:
        Constant Field Values

    - #### UTF_32

          public static final java.lang.String UTF_32

      Java friendly UTF-32 encoding name..
      See Also:
        Constant Field Values

    - #### UTF_32BE

          public static final java.lang.String UTF_32BE

      Java friendly UTF-32 big endian encoding name..
      See Also:
        Constant Field Values

    - #### UTF_32LE

          public static final java.lang.String UTF_32LE

      Java friendly UTF-32 little endian encoding name..
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### BOMNewlineEncodingDetector

          public BOMNewlineEncodingDetector(java.io.InputStream inputStream)

      Create a new BOMNewlineEncodingDetector from an `InputStream`. Cannot detect `BOMNewlineEncodingDetector.NewlineType` unless a valid encoding is detected.
      Parameters:
        `inputStream` - the input stream

    - #### BOMNewlineEncodingDetector

          public BOMNewlineEncodingDetector(java.io.InputStream inputStream,
                                    java.lang.String defaultEncoding)

      Create a new BOMNewlineEncodingDetector from an `InputStream` and a user provided encoding. This BOMNewlineEncodingDetector can convert the input bytes to Unicode for detection of the `BOMNewlineEncodingDetector.NewlineType`
      Parameters:
        `inputStream` - the input stream
        `defaultEncoding` - the default encoding

    - #### BOMNewlineEncodingDetector

          public BOMNewlineEncodingDetector(java.io.InputStream inputStream,
                                    java.nio.charset.Charset defaultEncoding)

  - ### Method Detail

    - #### getNewlineType

          public static BOMNewlineEncodingDetector.NewlineType getNewlineType(java.lang.CharSequence text)

      Static helper method for detecting newline type used in a run of text.
      Parameters:
        `text` - - text which includes newlines.
      Returns:
        the detected or guessed `BOMNewlineEncodingDetector.NewlineType`

    - #### getNewlineType

          public BOMNewlineEncodingDetector.NewlineType getNewlineType()

      Detects newline type using the inputStream itself.
      Returns:
        the detected or guessed `BOMNewlineEncodingDetector.NewlineType`

    - #### getInputStream

          public java.io.InputStream getInputStream()

      Get the input stream pased in to the constructor
      Returns:
        the `InputStream`

    - #### getEncoding

          public java.lang.String getEncoding()

      Get the guessed encoding or if encoding couldn't be guessed return the user supplied encoding. If no user supplied encoding is found use ISO_8859_1.
      Returns:
        the guessed or user supplied encoding.

    - #### getEncodingSpecificationInfo

          public java.lang.String getEncodingSpecificationInfo()

      Return a short description of the encoding.
      Returns:
        String containing the specification.

    - #### isDefinitive

          public boolean isDefinitive()

      Are we confident of the document encoding?
      Returns:
        true if the encoding is obvious from the BOM or bytes, false if the encoding must be guessed.

    - #### detectBom

          public void detectBom()

    - #### detectAndRemoveBom

          public void detectAndRemoveBom()

    - #### getDefaultEncoding

          public java.lang.String getDefaultEncoding()

      Get the defaultEncoding set by the user.
      Returns:
        String representation of the encoding

    - #### setDefaultEncoding

          public void setDefaultEncoding(java.lang.String defaultEncoding)

      Set the default encoding.
      Parameters:
        `defaultEncoding` - default encoding

    - #### hasBom

          public boolean hasBom()

      Does this document have a byte order mark?
      Returns:
        true if there is a BOM, false otherwise.

    - #### hasUtf8Bom

          public boolean hasUtf8Bom()

      Indicates if the guessed encoding is UTF-8 and this file has a BOM.
      Returns:
        True if the guessed encoding is UTF-8 and this file has a BOM, false otherwise.

    - #### hasUtf7Bom

          public boolean hasUtf7Bom()

      Does this document have a UTF-7 byte order mark?
      Returns:
        true if there is a BOM, false otherwise.

    - #### isAutodetected

          public boolean isAutodetected()

      Indicates if the guessed encoding was auto-detected. If not it is the default encoding that was provided.
      Returns:
        True if the guessed encoding was auto-detected, false if not.

    - #### getBomSize

          public int getBomSize()

      Gets the number of bytes used by the Byte-Order-mark in this document.
      Returns:
        The byte size of the BOM in this document.

    - #### hasUtf8Encoding

          public boolean hasUtf8Encoding()