You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/09/23 15:39:29 UTC

[GitHub] [kafka] LinShunKang commented on a diff in pull request #12545: KIP-863: Reduce Fetcher#parseRecord() memory copy

LinShunKang commented on code in PR #12545:
URL: https://github.com/apache/kafka/pull/12545#discussion_r978814376


##########
clients/src/main/java/org/apache/kafka/common/serialization/StringDeserializer.java:
##########
@@ -27,27 +30,45 @@
  *  value.deserializer.encoding or deserializer.encoding. The first two take precedence over the last.
  */
 public class StringDeserializer implements Deserializer<String> {
+
     private String encoding = StandardCharsets.UTF_8.name();
 
     @Override
     public void configure(Map<String, ?> configs, boolean isKey) {
-        String propertyName = isKey ? "key.deserializer.encoding" : "value.deserializer.encoding";
+        final String propertyName = isKey ? "key.deserializer.encoding" : "value.deserializer.encoding";
         Object encodingValue = configs.get(propertyName);
-        if (encodingValue == null)
+        if (encodingValue == null) {
             encodingValue = configs.get("deserializer.encoding");
-        if (encodingValue instanceof String)
+        }
+
+        if (encodingValue instanceof String) {
             encoding = (String) encodingValue;
+        }
     }
 
     @Override
     public String deserialize(String topic, byte[] data) {
         try {
-            if (data == null)
-                return null;
-            else
-                return new String(data, encoding);
+            return data == null ? null : new String(data, encoding);
         } catch (UnsupportedEncodingException e) {
             throw new SerializationException("Error when deserializing byte[] to string due to unsupported encoding " + encoding);
         }
     }
+
+    @Override
+    public String deserialize(String topic, Headers headers, ByteBuffer data) {
+        if (data == null) {
+            return null;
+        }
+
+        try {
+            if (data.hasArray()) {
+                return new String(data.array(), data.position() + data.arrayOffset(), data.remaining(), encoding);
+            } else {

Review Comment:
   Did you mean?
   ```
   try {
       if (data.hasArray()) {
           return new String(data.array(), data.position() + data.arrayOffset(), data.remaining(), encoding);
       } 
       return new String(Utils.toArray(data), encoding);
   } catch (UnsupportedEncodingException e) {
       throw new SerializationException("Error when deserializing ByteBuffer to string due to unsupported encoding " + encoding);
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org