Class BomUtil


  • final class BomUtil
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static int POTENTIAL_BOM_SIZE
      The maximum number of bytes a BOM header can have.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BomUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static java.util.Optional<BomHeader> detectCharset​(byte[] buf)
      Detects the character encoding of a byte array based on the presence of a Byte Order Mark (BOM) header.
      (package private) static java.util.Optional<BomHeader> detectCharset​(java.nio.file.Path file)
      Detects the character encoding of a file based on the presence of a Byte Order Mark (BOM) header.
      (package private) static java.io.Reader openReader​(java.nio.file.Path file, java.nio.charset.Charset defaultCharset)
      Opens a Reader for the given file, skipping a BOM header if present.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • POTENTIAL_BOM_SIZE

        static final int POTENTIAL_BOM_SIZE
        The maximum number of bytes a BOM header can have.
        See Also:
        Constant Field Values
    • Constructor Detail

      • BomUtil

        private BomUtil()
    • Method Detail

      • detectCharset

        static java.util.Optional<BomHeader> detectCharset​(byte[] buf)
        Detects the character encoding of a byte array based on the presence of a Byte Order Mark (BOM) header. The method supports the following BOM headers:
        • UTF-8 : EF BB BF
        • UTF-16 BE: FE FF
        • UTF-16 LE: FF FE
        • UTF-32 BE: 00 00 FE FF
        • UTF-32 LE: FF FE 00 00

        See Byte order mark

        Parameters:
        buf - the byte array to detect the character encoding from
        Returns:
        an Optional containing the detected BomHeader if a BOM header is found, or an empty Optional if no BOM header is found
      • detectCharset

        static java.util.Optional<BomHeader> detectCharset​(java.nio.file.Path file)
                                                    throws java.io.IOException
        Detects the character encoding of a file based on the presence of a Byte Order Mark (BOM) header.
        Parameters:
        file - the file to detect the character encoding from
        Returns:
        an Optional containing the detected BomHeader if a BOM header is found, or an empty Optional if no BOM header is found
        Throws:
        java.io.IOException - if an I/O error occurs reading the file
      • openReader

        static java.io.Reader openReader​(java.nio.file.Path file,
                                         java.nio.charset.Charset defaultCharset)
                                  throws java.io.IOException
        Opens a Reader for the given file, skipping a BOM header if present. If no BOM header is present, the defaultCharset is used.
        Parameters:
        file - the file to open a Reader for
        defaultCharset - the default charset to use if no BOM header is present
        Returns:
        a Reader for the given file
        Throws:
        java.io.IOException - if an I/O error occurs opening the file