public void testMultipleLargeStringsExceedingBufferSizeDelimited() throws Exception
    {
        LinkedBuffer buffer = LinkedBuffer.allocate(256);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        WriteSession session = new WriteSession(buffer, out);
        String utf8OneByte = repeatChar('a', 1024);
        String utf8TwoBytes = repeatChar((char) 0x7ff, 1024 / 2);
        String utf8ThreeBytes = repeatChar((char) 0x800, 1024 / 3);

        writeToSession(utf8OneByte, utf8TwoBytes, utf8ThreeBytes, session, true);
        assertTrue(session.tail == session.head);
        // temporary buffers will remain
        assertTrue(session.tail.next != null);
        // flush remaining
        LinkedBuffer.writeTo(out, buffer);
        // clear
        buffer.clear();

        byte[] data = out.toByteArray();

        LinkedBuffer buffer2 = LinkedBuffer.allocate(256);
        WriteSession session2 = new WriteSession(buffer2);

        writeToSession(utf8OneByte, utf8TwoBytes, utf8ThreeBytes, session2, true);

        byte[] data2 = session2.toByteArray();

        // We cannot do a direct STRING.deser, since it's not supposed to handle
        // multiple fields. Thus, in order to test this, we have to iterate through each
        // field, and compare the STRING.deser of that. Since we know the length ahead of time,
        // we can do simple verifications to make life easier.

        // Make sure the output bytes are the same.
        assertEquals(data.length, data2.length);

        // Make sure the data is identical.
        for (int i = 0; i < data.length; i++)
            assertEquals(data[i], data2[i]);

        // Now that we've confirmed they're identical, doing checks on the
        // validity is the same as doing it on the other.
        //
        // We will check that the VarDelimited value was encoded properly,
        // and that we can use that value to properly deserialize the String.

        int offset = 0;

        checkEncodedStringWithVarDelimited(data, utf8OneByte, offset, 1024);
        offset += 1024 + 2;
        checkEncodedStringWithVarDelimited(data, utf8OneByte, offset, 1024);
        offset += 1024 + 2;

        checkEncodedStringWithVarDelimited(data, utf8TwoBytes, offset, 1024);
        offset += 1024 + 2;
        checkEncodedStringWithVarDelimited(data, utf8TwoBytes, offset, 1024);
        offset += 1024 + 2;

        // These guy's length is actually 1023, because 1024/3 is 341.
        checkEncodedStringWithVarDelimited(data, utf8ThreeBytes, offset, 1023);
        offset += 1023 + 2;
        checkEncodedStringWithVarDelimited(data, utf8ThreeBytes, offset, 1023);
        offset += 1023 + 2;

        assertEquals(offset, data.length);
    }
Tests for streaming UTF-8 Encoding

@author David Yu
@created Sep 18, 2010
protostuff-api/src/test/java/io/protostuff/StreamedStringSerializerTest.java