private static LinkedBuffer writeUTF8Escaped(final CharSequence str, final WriteSink sink,
            final WriteSession session, LinkedBuffer lb) throws IOException
    {
        final int len = str.length();
        if (len == 0)
            return lb;

        byte[] buffer = lb.buffer;
        int limit = buffer.length, offset = lb.offset, size = len;

        for (int i = 0; i < len; i++)
        {
            final char c = str.charAt(i);
            if (c < 0x0080)
            {
                final int escape = sOutputEscapes[c];
                // System.out.print(c + "|" + escape + " ");
                if (escape == 0)
                {
                    // nothing to escape
                    if (offset == limit)
                    {
                        lb.offset = offset;
                        lb = sink.drain(session, lb);
                        offset = lb.offset;
                        buffer = lb.buffer;
                        limit = buffer.length;
                    }
                    // ascii
                    buffer[offset++] = (byte) c;
                }
                else if (escape < 0)
                {
                    // hex escape
                    if (offset + 6 > limit)
                    {
                        lb.offset = offset;
                        lb = sink.drain(session, lb);
                        offset = lb.offset;
                        buffer = lb.buffer;
                        limit = buffer.length;
                    }

                    final int value = -(escape + 1);

                    buffer[offset++] = (byte) '\\';
                    buffer[offset++] = (byte) 'u';
                    buffer[offset++] = (byte) '0';
                    buffer[offset++] = (byte) '0';
                    buffer[offset++] = HEX_BYTES[value >> 4];
                    buffer[offset++] = HEX_BYTES[value & 0x0F];

                    size += 5;
                }
                else
                {
                    if (offset + 2 > limit)
                    {
                        lb.offset = offset;
                        lb = sink.drain(session, lb);
                        offset = lb.offset;
                        buffer = lb.buffer;
                        limit = buffer.length;
                    }

                    buffer[offset++] = (byte) '\\';
                    buffer[offset++] = (byte) escape;

                    size++;
                }
            }
            else if (c < 0x0800)
            {
                if (offset + 2 > limit)
                {
                    lb.offset = offset;
                    lb = sink.drain(session, lb);
                    offset = lb.offset;
                    buffer = lb.buffer;
                    limit = buffer.length;
                }

                buffer[offset++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
                buffer[offset++] = (byte) (0x80 | ((c >> 0) & 0x3F));
                size++;
            }
            else
            {
                if (offset + 3 > limit)
                {
                    lb.offset = offset;
                    lb = sink.drain(session, lb);
                    offset = lb.offset;
                    buffer = lb.buffer;
                    limit = buffer.length;
                }

                buffer[offset++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
                buffer[offset++] = (byte) (0x80 | ((c >> 6) & 0x3F));
                buffer[offset++] = (byte) (0x80 | ((c >> 0) & 0x3F));
                size += 2;
            }
        }

        session.size += size;
        lb.offset = offset;

        return lb;
    }
Returns true if the last written field was a repeated field.
protostuff-json/src/main/java/io/protostuff/JsonXOutput.java