char[] decode(final ByteBuf in)
    {
        final CharBuffer charBuffer = CharBuffer.allocate(in.capacity());
        encoder.reset();
        final ByteBuffer nioBuffer = in.nioBuffer();
        encoder.decode(nioBuffer, charBuffer, false);
        final char[] buf = new char[charBuffer.position()];
        charBuffer.flip();
        charBuffer.get(buf);

        // Netty won't update the reader-index of the original buffer when its nio-buffer representation is read from. Adjust the position of the original buffer.
        in.readerIndex(nioBuffer.position());

        return buf;
    }
@Test
    public void testDecodePartialMultibyteString() throws Exception
    {
        final byte[] multibyteCharacter = "\u3053\u308C".getBytes(StandardCharsets.UTF_8); // two 3-byte characters.
        assert multibyteCharacter.length == 6;
        final XMLLightweightParser parser = new XMLLightweightParser();
        final ByteBuf in = ByteBufAllocator.DEFAULT.buffer(10);

        // Execute system under test.
        in.writeBytes(Arrays.copyOfRange(multibyteCharacter, 0, 5));
        final char[] result = parser.decode(in);

        // Verify results.
        assertEquals(1, result.length);
        assertEquals('\u3053', result[0]);
        assertEquals(3, in.readerIndex()); // Even though we tried to read 5 bytes, the reader index should be on the first byte after the character that was successfully read!
    }