From 16fec54e75215768f49286efd596bd8bc006dc16 Mon Sep 17 00:00:00 2001 From: wyok Date: Wed, 8 Jun 2016 11:59:01 +0800 Subject: [PATCH] fix bug KafkaNet.Common.Extensions public static byte[] ToIntSizedBytes(this string value) and public static byte[] ToInt16SizedBytes(this string value) These kafka STRING should use the bytes length, not string length. When we use UTF8 encode the none-ascii characters such as Chinese, the "string.Length" is the number of characters, not the real byte length. reffer: http://kafka.apache.org/protocol.html#protocol_types https://msdn.microsoft.com/en-us/library/system.strings(v=vs.110).aspx --- src/kafka-net/Common/Extensions.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/kafka-net/Common/Extensions.cs b/src/kafka-net/Common/Extensions.cs index d7106e3f..730d634b 100644 --- a/src/kafka-net/Common/Extensions.cs +++ b/src/kafka-net/Common/Extensions.cs @@ -16,8 +16,10 @@ public static byte[] ToIntSizedBytes(this string value) { if (string.IsNullOrEmpty(value)) return (-1).ToBytes(); - return value.Length.ToBytes() - .Concat(value.ToBytes()) + var bytes = value.ToBytes(); + + return bytes.Length.ToBytes() + .Concat(bytes) .ToArray(); } @@ -25,8 +27,10 @@ public static byte[] ToInt16SizedBytes(this string value) { if (string.IsNullOrEmpty(value)) return (-1).ToBytes(); - return ((Int16)value.Length).ToBytes() - .Concat(value.ToBytes()) + var bytes = value.ToBytes(); + + return ((Int16)bytes.Length).ToBytes() + .Concat(bytes) .ToArray(); }