Class ByteOrderMarkUtil

java.lang.Object
de.xima.fc.utils.ByteOrderMarkUtil

public final class ByteOrderMarkUtil extends Object
Utility class for working with byte order marks.
Since:
7.0.0
Author:
XIMA MEDIA GmbH
  • Constructor Details

    • ByteOrderMarkUtil

      public ByteOrderMarkUtil()
  • Method Details

    • getByteOrderMarkBytes

      @Nonnull public static byte[] getByteOrderMarkBytes(@Nonnull Charset charset)
      Parameters:
      charset - Charset to check.
      Returns:
      The bytes representing the byte order mark in that charset. Empty byte array when the charset does not support a byte order mark.
    • getByteOrderMarkSupport

      @Nonnull public static EByteOrderMarkSupport getByteOrderMarkSupport(@Nonnull Charset charset)
      Parameters:
      charset - Charset to check.
      Returns:
      The byte order mark support for the given charset.
    • isByteOrderMarkSupported

      @Nonnull public static boolean isByteOrderMarkSupported(@Nonnull Charset charset)
      Parameters:
      charset - Charset to check.
      Returns:
      true if the given charset supports byte order marks.
    • toBytes

      @Nonnull public static byte[] toBytes(@Nullable String data, @Nonnull Charset charset, EByteOrderMarkMode mode)
      Converts the given string to its byte representations according to the given charset. Optionally adds a BOM (byte-order mark) when supported by the charset. Only UTF-1, UTF-7, UTF_8, UTF-16 and UTF-32 charsets support this byte mark. In particular StandardCharsets.UTF_16BE and StandardCharsets.UTF_16LE require no BOM mark to be present. No byte order mark is added when the given data is null or empty.
      Parameters:
      data - String to encode. When the first character is a BOM, it is removed.
      charset - Charset to use.
      mode - When ADD, always adds the BOM (if missing) when the charset is a unicode charset. If REMOVE, removes the BOM so that it is not present in the returned byte array. When AUTO (or null), only adds the BOM when required by the charset.
      Returns:
      The bytes representing the data according to the given charset, optionally with a byte-order mark according to the given useBom. Empty byte array when the given data is null or empty.
      Throws:
      NullPointerException - When the given charset is null.