private static LinkedBuffer writeUTF8Escaped(final byte[] input, int inStart,
            int inLen, final WriteSink sink, final WriteSession session,
            LinkedBuffer lb) throws IOException
    {
        int lastStart = inStart;

        for (int i = 0; i < inLen; i++)
        {
            final byte b = input[inStart++];
            if (b > 0x7f)
                continue;

            // ascii
            final int escape = sOutputEscapes[b];
            if (escape == 0)
            {
                // nothing to escape
                continue;
            }

            if (escape < 0)
            {
                // hex escape

                // dump the bytes before this offset.
                final int dumpLen = inStart - lastStart - 1;
                if (dumpLen != 0)
                    lb = sink.writeByteArray(input, lastStart, dumpLen, session, lb);

                // update
                lastStart = inStart;

                if (lb.offset + 6 > lb.buffer.length)
                    lb = sink.drain(session, lb);

                final int value = -(escape + 1);

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

                session.size += 6;
            }
            else
            {
                // dump the bytes before this offset.
                final int dumpLen = inStart - lastStart - 1;
                if (dumpLen != 0)
                    lb = sink.writeByteArray(input, lastStart, dumpLen, session, lb);

                // update
                lastStart = inStart;

                if (lb.offset + 2 > lb.buffer.length)
                    lb = sink.drain(session, lb);

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

                session.size += 2;
            }
        }

        final int remaining = inStart - lastStart;

        return remaining == 0 ? lb : sink.writeByteArray(input, lastStart, remaining,
                session, lb);
    }
Returns true if the last written field was a repeated field.
protostuff-json/src/main/java/io/protostuff/JsonXOutput.java