ReadInt16() public method

Reads two-bytes signed integer from the current stream using big endian bytes order and advances the stream position by two bytes
public ReadInt16 ( ) : short
return short
        public void GetBytesValid()
        {
            const string topicName = "topic";
            var requestInfo = new Dictionary<string, List<PartitionOffsetRequestInfo>>();
            requestInfo[topicName] = new List<PartitionOffsetRequestInfo>() { new PartitionOffsetRequestInfo(0, OffsetRequest.LatestTime, 10) };
            var request = new OffsetRequest(requestInfo);

            // format = len(request) + requesttype + version + correlation id + client id + replica id + request info count + request infos
            int count = 2 + 2 + 4 + 2 + 4 + 4 + 4 +
                        BitWorks.GetShortStringLength("topic", AbstractRequest.DefaultEncoding) + 4 + 4 + 8 + 4;
            var ms = new MemoryStream();
            request.WriteTo(ms);
            byte[] bytes = ms.ToArray();
            Assert.IsNotNull(bytes);
            Assert.AreEqual(count, bytes.Length);

            var reader = new KafkaBinaryReader(ms);
            reader.ReadInt32().Should().Be(count - 4); // length
            reader.ReadInt16().Should().Be((short)RequestTypes.Offsets); // request type
            reader.ReadInt16().Should().Be(0); // version
            reader.ReadInt32().Should().Be(0); // correlation id
            string.IsNullOrEmpty(reader.ReadShortString()).Should().BeTrue(); // client id
            reader.ReadInt32().Should().Be(-1); // replica id
            reader.ReadInt32().Should().Be(1); // request info count
            reader.ReadShortString().Should().Be("topic");
            reader.ReadInt32().Should().Be(1); // info count
            reader.ReadInt32().Should().Be(0); // partition id
            reader.ReadInt64().Should().Be(OffsetRequest.LatestTime); // time
            reader.ReadInt32().Should().Be(10); // max offset
        }
Ejemplo n.º 2
0
 public static string ReadShortString(KafkaBinaryReader reader, string encoding)
 {
     var size = reader.ReadInt16();
     if (size < 0)
     {
         return null;
     }
     var bytes = reader.ReadBytes(size);
     Encoding encoder = Encoding.GetEncoding(encoding);
     return encoder.GetString(bytes);
 }
Ejemplo n.º 3
0
        public static string ParseFrom(KafkaBinaryReader reader, int count)
        {
            Guard.Assert<ArgumentNullException>(() => reader != null);

            var sb = new StringBuilder();
            sb.Append("Request size: ");
            sb.Append(reader.ReadInt32());
            sb.Append(", RequestId: ");
            short reqId = reader.ReadInt16();
            sb.Append(reqId);
            sb.Append("(");
            sb.Append((RequestTypes)reqId);
            sb.Append("), Single Requests: {");
            int i = 1;
            while (reader.BaseStream.Position != reader.BaseStream.Length)
            {
                sb.Append("Request ");
                sb.Append(i);
                sb.Append(" {");
                int msgSize = 0;
                sb.Append(ProducerRequest.ParseFrom(reader, msgSize));
                sb.AppendLine("} ");
                i++;
            }

            return sb.ToString();
        }
Ejemplo n.º 4
0
        public static string ParseFrom(KafkaBinaryReader reader, int count, bool skipReqInfo = false)
        {
            Guard.Assert<ArgumentNullException>(() => reader != null);
            var sb = new StringBuilder();

            if (!skipReqInfo)
            {
                sb.Append("Request size: ");
                sb.Append(reader.ReadInt32());
                sb.Append(", RequestId: ");
                short reqId = reader.ReadInt16();
                sb.Append(reqId);
                sb.Append("(");
                sb.Append((RequestTypes)reqId);
                sb.Append(")");
            }

            sb.Append(", Topic: ");
            string topic = reader.ReadTopic(DefaultEncoding);
            sb.Append(topic);
            sb.Append(", Partition: ");
            sb.Append(reader.ReadInt32());
            sb.Append(", Set size: ");
            sb.Append(reader.ReadInt32());
            int size = count - DefaultHeaderSize - GetTopicLength(topic);
            sb.Append(", Set {");
            sb.Append(BufferedMessageSet.ParseFrom(reader, size));
            sb.Append("}");
            return sb.ToString();
        }
 internal static PartitionData ParseFrom(KafkaBinaryReader reader)
 {
     var partition = reader.ReadInt32();
     var error = reader.ReadInt16();
     var highWatermark = reader.ReadInt64();
     var messageSetSize = reader.ReadInt32();
     var bufferedMessageSet = BufferedMessageSet.ParseFrom(reader, messageSetSize, partition);
     return new PartitionData(partition, ErrorMapper.ToError(error), bufferedMessageSet);
 }
        public static PartitionMetadata ParseFrom(KafkaBinaryReader reader, Dictionary<int, Broker> brokers)
        {
            var errorCode = reader.ReadInt16();
            var partitionId = reader.ReadInt32();
            var leaderId = reader.ReadInt32();
            Broker leader = null;
            if (leaderId != -1)
            {
                leader = brokers[leaderId];
            }

            // list of all replicas
            var numReplicas = reader.ReadInt32();
            var replicas = new List<Broker>();
            for (int i = 0; i < numReplicas; ++i)
            {
                replicas.Add(brokers[reader.ReadInt32()]);
            }

            // list of in-sync replicas
            var numIsr = reader.ReadInt32();
            var isrs = new List<Broker>();
            for (int i = 0; i < numIsr; ++i)
            {
                isrs.Add(brokers[reader.ReadInt32()]);
            }

            return new PartitionMetadata(partitionId, leader, replicas, isrs);
        }