From 2d9bf8b00588173b70988bccb57ce79737b8de66 Mon Sep 17 00:00:00 2001 From: vismaytiwari Date: Thu, 9 Jul 2026 12:12:58 +0530 Subject: [PATCH] KAFKA-19877: Clarify Deserializer ByteBuffer position/limit contract The deserialize(String, Headers, ByteBuffer) Javadoc stated only what callers and implementations cannot assume about the buffer (position, limit, capacity), leaving the readable region and side-effect expectations unspecified. This clarifies that the serialized bytes are the buffer's remaining() bytes and that implementations should not modify the buffer's position/limit/mark, since the caller may read the buffer again afterward (for example, to determine the serialized value size). Co-Authored-By: Claude Opus 4.8 --- .../org/apache/kafka/common/serialization/Deserializer.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/clients/src/main/java/org/apache/kafka/common/serialization/Deserializer.java b/clients/src/main/java/org/apache/kafka/common/serialization/Deserializer.java index cf24d858e5865..e03287843c35d 100644 --- a/clients/src/main/java/org/apache/kafka/common/serialization/Deserializer.java +++ b/clients/src/main/java/org/apache/kafka/common/serialization/Deserializer.java @@ -97,6 +97,12 @@ default T deserialize(String topic, Headers headers, byte[] data) { *

Similarly, if this method is overridden, the implementation cannot make any assumptions about the * passed in {@link ByteBuffer} either. * + *

The serialized bytes are the buffer's {@link ByteBuffer#remaining() remaining} bytes, i.e. those + * between its current {@code position} and {@code limit}. An implementation should read the data without + * changing the buffer's {@code position}, {@code limit}, or {@code mark}: the caller retains ownership of + * the buffer and may read it again after this method returns (for example, to determine the serialized + * size of the value), so mutating the buffer's state can corrupt unrelated processing. + * *

It is recommended to deserialize a {@code null} {@link ByteBuffer} to a {@code null} object. * *

Note that the passed in {@link Headers} may be empty, but never {@code null}.