From 3dff61a08271275d3c1121ed2dba1ea462336baf Mon Sep 17 00:00:00 2001 From: David Arthur Date: Thu, 3 Jan 2013 12:49:24 -0500 Subject: [PATCH] KAFKA-680 Use length of encoded byte array Instead of the String length in ApiUtils#writeShortString --- core/src/main/scala/kafka/api/ApiUtils.scala | 13 ++++++++----- 1 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/kafka/api/ApiUtils.scala b/core/src/main/scala/kafka/api/ApiUtils.scala index ba1d199..a6b0328 100644 --- a/core/src/main/scala/kafka/api/ApiUtils.scala +++ b/core/src/main/scala/kafka/api/ApiUtils.scala @@ -31,11 +31,14 @@ object ApiUtils { def writeShortString(buffer: ByteBuffer, string: String) { if(string == null) { buffer.putShort(-1) - } else if(string.length > Short.MaxValue) { - throw new KafkaException("String exceeds the maximum size of " + Short.MaxValue + ".") } else { - buffer.putShort(string.length.asInstanceOf[Short]) - buffer.put(string.getBytes(ProtocolEncoding)) + val encodedString = string.getBytes(ProtocolEncoding) + if(encodedString.length > Short.MaxValue) { + throw new KafkaException("String exceeds the maximum size of " + Short.MaxValue + ".") + } else { + buffer.putShort(encodedString.length.asInstanceOf[Short]) + buffer.put(encodedString) + } } } @@ -89,4 +92,4 @@ object ApiUtils { else value } -} \ No newline at end of file +} -- 1.7.5.4