private static void encodeUtf8Component(StringBuilder buf, String s, int start) {
        final int end = s.length();
        if (start == end) {
            return;
        }

        final char[] tmp = new char[12];
        tmp[0] = tmp[3] = tmp[6] = tmp[9] = '%'; // Pre-set '%' so we don't have to set anymore.

        int i = start;
        for (;;) {
            char c = s.charAt(i++);
            if (c < 0x80) {
                if (c == ' ') {
                    buf.append('+');
                } else {
                    tmp[2] = UPPER_HEX_DIGITS[c & 0xF];
                    tmp[1] = UPPER_HEX_DIGITS[c >>> 4];
                    buf.append(tmp, 0, 3);
                }
            } else if (c < 0x800) {
                tmp[5] = UPPER_HEX_DIGITS[c & 0xF];
                c >>>= 4;
                tmp[4] = UPPER_HEX_DIGITS[0x8 | (c & 0x3)];
                c >>>= 2;
                tmp[2] = UPPER_HEX_DIGITS[c & 0xF];
                tmp[1] = UPPER_HEX_DIGITS[0xC | (c >>> 4)];
                buf.append(tmp, 0, 6);
            } else if (!StringUtil.isSurrogate(c)) {
                tmp[8] = UPPER_HEX_DIGITS[c & 0xF];
                c >>>= 4;
                tmp[7] = UPPER_HEX_DIGITS[0x8 | (c & 0x3)];
                c >>>= 2;
                tmp[5] = UPPER_HEX_DIGITS[c & 0xF];
                c >>>= 4;
                tmp[4] = UPPER_HEX_DIGITS[0x8 | (c & 0x3)];
                tmp[2] = UPPER_HEX_DIGITS[(c >>> 2) & 0xF];
                tmp[1] = 'E';
                buf.append(tmp, 0, 9);
            } else if (!Character.isHighSurrogate(c) || i == end) {
                buf.append(UTF_UNKNOWN);
            } else {
                final char c2 = s.charAt(i++);
                if (!Character.isLowSurrogate(c2)) {
                    buf.append(UTF_UNKNOWN);
                    if (Character.isHighSurrogate(c2)) {
                        buf.append(UTF_UNKNOWN);
                    } else {
                        tmp[2] = UPPER_HEX_DIGITS[c2 & 0xF];
                        tmp[1] = UPPER_HEX_DIGITS[c2 >>> 4];
                        buf.append(tmp, 0, 3);
                    }
                } else {
                    int codePoint = Character.toCodePoint(c, c2);
                    // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G2630.
                    tmp[11] = UPPER_HEX_DIGITS[codePoint & 0xF];
                    codePoint >>>= 4;
                    tmp[10] = UPPER_HEX_DIGITS[0x8 | (codePoint & 0x3)];
                    codePoint >>>= 2;
                    tmp[8] = UPPER_HEX_DIGITS[codePoint & 0xF];
                    codePoint >>>= 4;
                    tmp[7] = UPPER_HEX_DIGITS[0x8 | (codePoint & 0x3)];
                    codePoint >>>= 2;
                    tmp[5] = UPPER_HEX_DIGITS[codePoint & 0xF];
                    codePoint >>>= 4;
                    tmp[4] = UPPER_HEX_DIGITS[0x8 | (codePoint & 0x3)];
                    tmp[2] = UPPER_HEX_DIGITS[(codePoint >>> 2) & 0xF];
                    tmp[1] = 'F';
                    buf.append(tmp, 0, 12);
                }
            }

            // Find and append the safe region as-is.
            final int nextUnsafeOctetIndex = indexOfUnsafeOctet(s, i);
            if (nextUnsafeOctetIndex < 0) {
                if (i != end) {
                    buf.append(s, i, end);
                }
                break;
            }

            if (nextUnsafeOctetIndex != i) {
                buf.append(s, i, nextUnsafeOctetIndex);
                i = nextUnsafeOctetIndex;
            }
        }
    }
Encodes a query component (name or value). The octet at {@code start} must be an unsafe octet.
core/src/main/java/com/linecorp/armeria/internal/common/PercentEncoder.java