Class EncodingUtils


  • @Internal
    public abstract class EncodingUtils
    extends Object
    General utilities for string-encoding. This class is used to avoid additional dependencies to other projects.
    • Method Detail

      • escapeBackticks

        public static String escapeBackticks​(String s)
      • escapeSingleQuotes

        public static String escapeSingleQuotes​(String s)
      • escapeIdentifier

        public static String escapeIdentifier​(String s)
      • encodeObjectToString

        public static String encodeObjectToString​(Object serializable)
      • decodeStringToObject

        public static <T extends Serializable> T decodeStringToObject​(String base64String,
                                                                      Class<T> baseClass)
      • encodeBytesToBase64

        public static String encodeBytesToBase64​(byte[] bytes)
      • decodeBase64ToBytes

        public static byte[] decodeBase64ToBytes​(String base64)
      • encodeStringToBase64

        public static String encodeStringToBase64​(String string)
      • decodeBase64ToString

        public static String decodeBase64ToString​(String base64)
      • md5

        public static byte[] md5​(String string)
      • hex

        public static String hex​(byte[] bytes)
      • unhex

        public static byte[] unhex​(byte[] bytes)
        Converts an array of characters representing hexadecimal values into an array of bytes of those same values. E.g. unhex("12".getBytes()) returns new byte[]{0x12}.

        The returned array will be half the length of the passed array, as it takes two characters to represent any given byte. If the input array has an odd length, the first byte is handled separately and set to 0.

        Unlike decodeHex(String), this method does not throw an exception for odd-length inputs or invalid characters. Instead, it returns null if invalid characters are encountered.

        Parameters:
        bytes - An array of characters containing hexadecimal digits.
        Returns:
        A byte array containing the binary data decoded from the supplied char array, or null if the input contains invalid hexadecimal characters.
      • decodeHex

        public static byte[] decodeHex​(String str)
                                throws TableException
        Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The returned array will be half the length of the passed array, as it takes two characters to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.

        Copied from https://github.com/apache/commons-codec/blob/master/src/main/java/org/apache/commons/codec/binary/Hex.java.

        Parameters:
        str - An array of characters containing hexadecimal digits
        Returns:
        A byte array to contain the binary data decoded from the supplied char array.
        Throws:
        TableException - Thrown if an odd number of characters or illegal characters are supplied
      • repeat

        public static String repeat​(String str,
                                    int repeat)
        Repeat a String repeat times to form a new String.
         StringUtils.repeat(null, 2) = null
         StringUtils.repeat("", 0)   = ""
         StringUtils.repeat("", 2)   = ""
         StringUtils.repeat("a", 3)  = "aaa"
         StringUtils.repeat("ab", 2) = "abab"
         StringUtils.repeat("a", -2) = ""
         
        Parameters:
        str - the String to repeat, may be null
        repeat - number of times to repeat str, negative treated as zero
        Returns:
        a new String consisting of the original String repeated, null if null String input
      • repeat

        public static String repeat​(char ch,
                                    int repeat)
        Returns padding using the specified delimiter repeated to a given length.
         StringUtils.repeat('e', 0)  = ""
         StringUtils.repeat('e', 3)  = "eee"
         StringUtils.repeat('e', -2) = ""
         

        Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a pair of chars to be represented. If you are needing to support full I18N of your applications consider using repeat(String, int) instead.

        Parameters:
        ch - character to repeat
        repeat - number of times to repeat char, negative treated as zero
        Returns:
        String with repeated character
        See Also:
        repeat(String, int)
      • escapeJava

        public static String escapeJava​(String str)
        Escapes the characters in a String using Java String rules.

        Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

        So a tab becomes the characters '\\' and 't'.

        The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.

        Example:

         input string: He didn't say, "Stop!"
         output string: He didn't say, \"Stop!\"
         
        Parameters:
        str - String to escape values in, may be null
        Returns:
        String with escaped values, null if null string input