Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Status

Current state: "Under DiscussionDiscard"

Discussion thread: https://lists.apache.org/thread/l2zkd375818gkg5753xqhcqf4boqhbqm

...

Currently `ByteBufferSerializer#serialize(String, ByteBuffer)` has compatible problem, If the ByteBuffer contains only part of the ByteBuffer#capacity is 7 and only has 5 bytes data `ByteBufferSerializer#serialize(String, ByteBuffer)` will return all 7 bytes in the ByteBuffer instead of valid 5 bytes:

Code Block
languagejava
@Test
public void testByteBufferSerializer() {
    final byte[] bytes = "Hello".getBytes(UTF_8);
    final ByteBuffer buffer = ByteBuffer.allocate(7);
    buffer.put(bytes);

    try (final ByteBufferSerializer serializer = new ByteBufferSerializer()) {
        assertArrayEquals(bytes, serializer.serialize(topic, buffer));
    }
} 

...

  • If the old behavior is correct, then the new behavior is also correct.

Test Plan

Perform serialization tests with HeapByteBuffer&DirectByteBuffer of suitable and larger capacity:
Code Block
languagejava
    @Test
    public void testByteBufferSerializer() {
        final byte[] bytes = "Hello".getBytes(UTF_8);
        final ByteBuffer heapBuffer0 = ByteBuffer.allocate(bytes.length + 1).put(bytes);
        final ByteBuffer heapBuffer1 = ByteBuffer.allocate(bytes.length).put(bytes);
        final ByteBuffer heapBuffer2 = ByteBuffer.wrap(bytes);
        final ByteBuffer directBuffer0 = ByteBuffer.allocateDirect(bytes.length + 1).put(bytes);
        final ByteBuffer directBuffer1 = ByteBuffer.allocateDirect(bytes.length).put(bytes);
        try (final ByteBufferSerializer serializer = new ByteBufferSerializer()) {
            assertArrayEquals(bytes, serializer.serialize(topic, heapBuffer0));
            assertArrayEquals(bytes, serializer.serialize(topic, heapBuffer1));
            assertArrayEquals(bytes, serializer.serialize(topic, heapBuffer2));
            assertArrayEquals(bytes, serializer.serialize(topic, directBuffer0));
            assertArrayEquals(bytes, serializer.serialize(topic, directBuffer1));
        }
    }

...