Class EncodingUtils
- java.lang.Object
-
- org.apache.flink.table.utils.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 Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static byte[]
decodeBase64ToBytes(String base64)
static String
decodeBase64ToString(String base64)
static byte[]
decodeHex(String str)
Converts an array of characters representing hexadecimal values into an array of bytes of those same values.static <T extends Serializable>
TdecodeStringToObject(String base64String, Class<T> baseClass)
static <T extends Serializable>
TdecodeStringToObject(String base64String, Class<T> baseClass, ClassLoader classLoader)
static <T> T
decodeStringToObject(String encodedStr, ClassLoader classLoader)
static String
encodeBytesToBase64(byte[] bytes)
static String
encodeObjectToString(Serializable obj)
static String
encodeObjectToString(Object serializable)
static String
encodeStringToBase64(String string)
static String
escapeBackticks(String s)
static String
escapeIdentifier(String s)
static String
escapeJava(String str)
Escapes the characters in aString
using Java String rules.static String
escapeSingleQuotes(String s)
static String
hex(byte[] bytes)
static String
hex(String string)
static Class<?>
loadClass(String qualifiedName)
Deprecated.UseloadClass(String, ClassLoader)
instead, in order to explicitly provide the correct classloader.static Class<?>
loadClass(String qualifiedName, ClassLoader classLoader)
static byte[]
md5(String string)
static String
repeat(char ch, int repeat)
Returns padding using the specified delimiter repeated to a given length.static String
repeat(String str, int repeat)
Repeat a Stringrepeat
times to form a new String.static byte[]
unhex(byte[] bytes)
Converts an array of characters representing hexadecimal values into an array of bytes of those same values.
-
-
-
Method Detail
-
encodeObjectToString
public static String encodeObjectToString(Serializable obj)
-
decodeStringToObject
public static <T> T decodeStringToObject(String encodedStr, ClassLoader classLoader) throws IOException
- Throws:
IOException
-
decodeStringToObject
public static <T extends Serializable> T decodeStringToObject(String base64String, Class<T> baseClass)
-
decodeStringToObject
public static <T extends Serializable> T decodeStringToObject(String base64String, Class<T> baseClass, ClassLoader classLoader)
-
loadClass
public static Class<?> loadClass(String qualifiedName, ClassLoader classLoader)
-
loadClass
@Deprecated public static Class<?> loadClass(String qualifiedName)
Deprecated.UseloadClass(String, ClassLoader)
instead, in order to explicitly provide the correct classloader.
-
encodeBytesToBase64
public static String encodeBytesToBase64(byte[] bytes)
-
decodeBase64ToBytes
public static byte[] decodeBase64ToBytes(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())
returnsnew 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 Stringrepeat
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 nullrepeat
- 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
char
s to be represented. If you are needing to support full I18N of your applications consider usingrepeat(String, int)
instead.- Parameters:
ch
- character to repeatrepeat
- 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 aString
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
-
-