private static Utf8String newString(String string) {
        byte coder = StringUtil.getCoder(string);
        byte[] bytes = StringUtil.getBytes(string);
        // Fast path for trusted Latin-1
        boolean isLatin1 = StringUtil.isLatin1(coder);
        if (isLatin1) {
            int byteLength = bytes.length;
            return new Utf8String(
                    StringUtil.newString(bytes, coder),
                    bytes,
                    0,
                    byteLength,
                    byteLength);
        }
        // Slow path for UTF16
        int utf16CharLength = string.length();
        int maxBytesLength = utf16CharLength * MAX_BYTES_PER_UTF16_CHAR;
        byte[] out = new byte[maxBytesLength];
        int i = 0;
        char c;
        int charCount = 0;
        for (; i < utf16CharLength && (c = string.charAt(i)) < 0x80; i++) {
            out[i] = (byte) c;
            charCount++;
        }
        if (i == utf16CharLength) {
            return new Utf8String(
                    StringUtil.newString(bytes, coder),
                    out,
                    0,
                    utf16CharLength,
                    utf16CharLength);
        }
        int byteCount = i;
        for (; i < utf16CharLength; i++) {
            c = string.charAt(i);
            if (c < 0x80 && byteCount < maxBytesLength) {
                out[byteCount++] = (byte) c;
            } else if (c < 0x800 && byteCount <= maxBytesLength - 2) {
                // 11 bits, two UTF-8 bytes
                out[byteCount++] = (byte) ((0xF << 6) | (c >>> 6));
                out[byteCount++] = (byte) (0x80 | (0x3F & c));
            } else if ((c < Character.MIN_SURROGATE || Character.MAX_SURROGATE < c)
                    && byteCount <= maxBytesLength - 3) {
                // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes
                out[byteCount++] = (byte) ((0xF << 5) | (c >>> 12));
                out[byteCount++] = (byte) (0x80 | (0x3F & (c >>> 6)));
                out[byteCount++] = (byte) (0x80 | (0x3F & c));
            } else if (byteCount <= maxBytesLength - 4) {
                // Minimum code point represented by a surrogate pair is 0x10000, 17 bits,
                // four UTF-8 bytes
                char low;
                if (i + 1 == utf16CharLength
                        || !Character.isSurrogatePair(c, (low = string.charAt(++i)))) {
                    throw new IllegalArgumentException(
                            "Unpaired surrogate at the index "
                                    + (i - 1)
                                    + " of "
                                    + utf16CharLength);
                }
                int codePoint = Character.toCodePoint(c, low);
                out[byteCount++] = (byte) ((0xF << 4) | (codePoint >>> 18));
                out[byteCount++] = (byte) (0x80 | (0x3F & (codePoint >>> 12)));
                out[byteCount++] = (byte) (0x80 | (0x3F & (codePoint >>> 6)));
                out[byteCount++] = (byte) (0x80 | (0x3F & codePoint));
            } else {
                if ((Character.MIN_SURROGATE <= c && c <= Character.MAX_SURROGATE)
                        && (i + 1 == utf16CharLength
                                || !Character.isSurrogatePair(c, string.charAt(i + 1)))) {
                    throw new IllegalArgumentException(
                            "Unpaired surrogate at the index "
                                    + i
                                    + " of "
                                    + utf16CharLength);
                }
                throw new ArrayIndexOutOfBoundsException(
                        "Failed to write \""
                                + c
                                + "\" at the index "
                                + byteCount);
            }
            charCount++;
        }
        return new Utf8String(StringUtil.newString(bytes, coder), out, 0, byteCount, charCount);
    }
@see java.nio.charset.CharsetEncoder#maxBytesPerChar()
turms-server-common/src/main/java/im/turms/server/common/infra/lang/Utf8String.java