public static LinkedBuffer writeUTF8(final CharSequence str, final WriteSession session,
            final LinkedBuffer lb) throws IOException
    {
        final int len = str.length();
        if (len == 0)
            return lb;

        final byte[] buffer = lb.buffer;
        int limit = buffer.length, offset = lb.offset, i = 0;

        char c;
        do
        {
            c = str.charAt(i++);
            if (c < 0x0080)
            {
                if (offset == limit)
                {
                    session.size += (offset - lb.offset);
                    lb.offset = offset = session.flush(buffer, lb.start, offset - lb.start);
                }
                // ascii
                buffer[offset++] = (byte) c;
            }
            else if (c < 0x0800)
            {
                if (offset + 2 > limit)
                {
                    session.size += (offset - lb.offset);
                    lb.offset = offset = session.flush(buffer, lb.start, offset - lb.start);
                }

                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.
                if (offset + 4 > buffer.length)
                {
                    session.size += (offset - lb.offset);
                    lb.offset = offset = session.flush(buffer, lb.start, offset - lb.start);
                }

                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
            {
                if (offset + 3 > limit)
                {
                    session.size += (offset - lb.offset);
                    lb.offset = offset = session.flush(buffer, lb.start, offset - lb.start);
                }

                buffer[offset++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
                buffer[offset++] = (byte) (0x80 | ((c >> 6) & 0x3F));
                buffer[offset++] = (byte) (0x80 | ((c >> 0) & 0x3F));
            }
        } while (i < len);

        session.size += (offset - lb.offset);
        lb.offset = offset;

        return lb;
    }
Writes the utf8-encoded bytes from the string into the {@link LinkedBuffer}.
protostuff-api/src/main/java/io/protostuff/StreamedStringSerializer.java