Class UnicodeHelper

java.lang.Object
fr.becpg.repo.helper.UnicodeHelper

public final class UnicodeHelper extends Object
Utility methods for Unicode text sanitization.
Author:
beCPG
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Checks if a string holds any character that sanitizeBmp(String) strips, that is a supplementary character or an unpaired surrogate.
    private static boolean
    isStorableCodePoint(int codePoint)
    Checks whether a code point can be stored in a MySQL utf8/utf8mb3 column.
    static String
    Strips the characters that MySQL utf8/utf8mb3 tables cannot store, to prevent SQLException: non-BMP characters (code points > 0xFFFF, e.g. 4-byte UTF-8 emojis) and unpaired surrogates.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • UnicodeHelper

      private UnicodeHelper()
  • Method Details

    • sanitizeBmp

      public static String sanitizeBmp(String text)
      Strips the characters that MySQL utf8/utf8mb3 tables cannot store, to prevent SQLException: non-BMP characters (code points > 0xFFFF, e.g. 4-byte UTF-8 emojis) and unpaired surrogates.
      Parameters:
      text - input text
      Returns:
      sanitized text holding only storable code points, or original text if unchanged
    • isStorableCodePoint

      private static boolean isStorableCodePoint(int codePoint)
      Checks whether a code point can be stored in a MySQL utf8/utf8mb3 column. A supplementary code point needs 4 UTF-8 bytes, and an unpaired surrogate, which is a BMP code point on its own, has no valid UTF-8 encoding at all.
      Parameters:
      codePoint - code point to check
      Returns:
      true if the code point is a BMP code point other than a surrogate
    • containsNonBmp

      public static boolean containsNonBmp(String text)
      Checks if a string holds any character that sanitizeBmp(String) strips, that is a supplementary character or an unpaired surrogate.
      Parameters:
      text - text to check
      Returns:
      true if text contains at least one surrogate char