Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-4852: Fix ByteBufferSerializer#serialize(String, ByteBuffer) not compatible with offsets #12683

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,40 @@
*/
package org.apache.kafka.common.serialization;

import org.apache.kafka.common.utils.Utils;

import java.nio.ByteBuffer;

/**
* ByteBufferSerializer will not change ByteBuffer's mark, position and limit.
* And do not need to flip before call <i>serialize(String, ByteBuffer)</i>. For example:
*
* <blockquote>
* <pre>
* ByteBufferSerializer serializer = ...; // Create Serializer
* ByteBuffer buffer = ...; // Allocate ByteBuffer
* buffer.put(data); // Put data into buffer, do not need to flip
* serializer.serialize(topic, buffer); // Serialize buffer
* </pre>
* </blockquote>
*/
public class ByteBufferSerializer implements Serializer<ByteBuffer> {

@Override
public byte[] serialize(String topic, ByteBuffer data) {
if (data == null)
if (data == null) {
return null;

data.rewind();
}

if (data.hasArray()) {
byte[] arr = data.array();
final byte[] arr = data.array();
if (data.arrayOffset() == 0 && arr.length == data.remaining()) {
return arr;
}
}

byte[] ret = new byte[data.remaining()];
data.get(ret, 0, ret.length);
data.rewind();
return ret;
final ByteBuffer copyData = data.asReadOnlyBuffer();
copyData.flip();
return Utils.toArray(copyData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.LinkedList;
import java.util.Stack;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -368,4 +370,21 @@ private Serde<String> getStringSerde(String encoder) {

return Serdes.serdeFrom(serializer, deserializer);
}

@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));
}
}
}