static LinkedBuffer writeUTF8(final CharSequence str, int i, final int len,
            final WriteSession session, final LinkedBuffer lb)
    {
        final byte[] buffer = lb.buffer;
        for (int c = 0, offset = lb.offset, adjustableLimit = offset + len;; c = 0)
        {
            while (i != len && (c = str.charAt(i++)) < 0x0080)
                buffer[offset++] = (byte) c;

            if (i == len && c < 0x0080)
            {
                session.size += (offset - lb.offset);
                lb.offset = offset;
                return lb;
            }

            if (c < 0x0800)
            {
                if (++adjustableLimit > buffer.length)
                {
                    session.size += (offset - lb.offset);
                    lb.offset = offset;
                    return writeUTF8(str, i - 1, len, buffer, offset, buffer.length, session, lb);
                }

                buffer[offset++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
                buffer[offset++] = (byte) (0x80 | ((c >> 0) & 0x3F));
            }
            else if (Character.isHighSurrogate((char) c) && i < len && Character.isLowSurrogate((char) str.charAt(i)))
            {
                // We have a surrogate pair, so use the 4-byte encoding.
                adjustableLimit += 3;
                if (adjustableLimit > buffer.length)
                {
                    session.size += (offset - lb.offset);
                    lb.offset = offset;
                    return writeUTF8(str, i - 1, len, buffer, offset, buffer.length, session, lb);
                }

                int codePoint = Character.toCodePoint((char) c, (char) str.charAt(i));
                buffer[offset++] = (byte) (0xF0 | ((codePoint >> 18) & 0x07));
                buffer[offset++] = (byte) (0x80 | ((codePoint >> 12) & 0x3F));
                buffer[offset++] = (byte) (0x80 | ((codePoint >> 6) & 0x3F));
                buffer[offset++] = (byte) (0x80 | ((codePoint >> 0) & 0x3F));

                i++;
            }
            else
            {
                adjustableLimit += 2;
                if (adjustableLimit > buffer.length)
                {
                    session.size += (offset - lb.offset);
                    lb.offset = offset;
                    return writeUTF8(str, i - 1, len, buffer, offset, buffer.length, session, lb);
                }

                buffer[offset++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
                buffer[offset++] = (byte) (0x80 | ((c >> 6) & 0x3F));
                buffer[offset++] = (byte) (0x80 | ((c >> 0) & 0x3F));
            }
        }
    }
Fast path. The {@link LinkedBuffer}'s capacity is >= string length.
protostuff-api/src/main/java/io/protostuff/StringSerializer.java