Java Utililty Methods UTF8

List of utility methods to do UTF8

Description

The list of methods to do UTF8 are organized into topic(s).

Method

intutf8StringLength(final CharSequence sequence)
utf String Length
int count = 0;
for (int i = 0, len = sequence.length(); i < len; i++) {
    char ch = sequence.charAt(i);
    if (ch <= 0x7F) {
        count++;
    } else if (ch <= 0x7FF) {
        count += 2;
    } else if (Character.isHighSurrogate(ch)) {
...
intutf8StringSizeInBytes(String s)
Compute the size in bytes of the given string encoded as utf8.
int res = 0;
for (int i = 0; i < s.length(); ++i) {
    char c = s.charAt(i);
    int codepoint = c;
    if (isSurrogate(c)) {
        i++;
        char c2 = s.charAt(i);
        codepoint = Character.toCodePoint(c, c2);
...
byte[]utf8StringToByteArray(String dataString)
Creates a byte array for an UTF-8 encoded string.
if (dataString == null) {
    return null;
try {
    return dataString.getBytes("utf-8");
} catch (Exception ex) {
return null;
...
byte[]utf8StringToBytes(String string)
Turn a UTF-8 encoded string into an array of bytes
return asBytes(string, UTF_8);
Stringutf8ToString(byte[] data)
Decodes the specified UTF-8 sequence to a string.
return utf8ToString(data, 0, data.length);
Stringutf8URLDecode(String input)
Decodes the URL encoded input string, in UTF-8.
try {
    return decode(input, UTF_8.displayName());
} catch (UnsupportedEncodingException e) {
    return "";
Stringutf8urldecode(String text)
utfurldecode
StringBuilder result = new StringBuilder();
int p;
if (text != null && text.length() > 0) {
    text = text.toLowerCase();
    p = text.indexOf("%e");
    if (p == -1) {
        return text;
    while (p != -1) {
        result.append(text, 0, p);
        text = text.substring(p);
        if (Objects.equals(text, "") || text.length() < 9) {
            return result.toString();
        result.append(codetoword(text.substring(0, 9)));
        text = text.substring(9);
        p = text.indexOf("%e");
return result + text;
StringUtf8URLencode(String text)
Utf UR Lencode
StringBuffer result = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
    char c = text.charAt(i);
    if (c >= 0 && c <= 255) {
        result.append(c);
    } else {
        byte[] b = new byte[0];
        try {
...