private static String doEscape(byte[] valueBytes, int ri) {
        try (TemporaryThreadLocals tempThreadLocals = TemporaryThreadLocals.acquire()) {
            final byte[] escapedBytes = tempThreadLocals.byteArray(ri + (valueBytes.length - ri) * 3);
            // copy over the good bytes
            if (ri != 0) {
                System.arraycopy(valueBytes, 0, escapedBytes, 0, ri);
            }

            int wi = ri;
            for (; ri < valueBytes.length; ri++) {
                final byte b = valueBytes[ri];
                // Manually implement URL encoding, per the gRPC spec.
                if (isEscapingChar(b)) {
                    escapedBytes[wi] = '%';
                    escapedBytes[wi + 1] = HEX[(b >> 4) & 0xF];
                    escapedBytes[wi + 2] = HEX[b & 0xF];
                    wi += 3;
                    continue;
                }
                escapedBytes[wi++] = b;
            }

            //noinspection deprecation
            return new String(escapedBytes, 0, 0, wi);
        }
    }
Escapes the given byte array.

@param valueBytes the UTF-8 bytes
@param ri The reader index, pointed at the first byte that needs escaping.
grpc-protocol/src/main/java/com/linecorp/armeria/common/grpc/protocol/StatusMessageEscaper.java