예제 #1
0
파일: Message.cs 프로젝트: akutta/kafka-net
        /// <summary>
        /// Decode a byte[] that represents a collection of messages.
        /// </summary>
        /// <param name="messageSet">The byte[] encode as a message set from kafka.</param>
        /// <returns>Enumerable representing stream of messages decoded from byte[]</returns>
        public static IEnumerable <Message> DecodeMessageSet(byte[] messageSet)
        {
            var stream = new ReadByteStream(messageSet);


            while (stream.HasData)
            {
                //this checks that we have at least the minimum amount of data to retrieve a header
                if (stream.Available(MessageHeaderSize) == false)
                {
                    yield break;
                }

                var offset      = stream.ReadLong();
                var messageSize = stream.ReadInt();

                //if messagessize is greater than payload, our max buffer is insufficient.
                if ((stream.Payload.Length - MessageHeaderSize) < messageSize)
                {
                    throw new BufferUnderRunException(MessageHeaderSize, messageSize);
                }

                //if the stream does not have enough left in the payload, we got only a partial message
                if (stream.Available(messageSize) == false)
                {
                    yield break;
                }

                foreach (var message in DecodeMessage(offset, stream.ReadBytesFromStream(messageSize)))
                {
                    yield return(message);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Decode a byte[] that represents a collection of messages.
        /// </summary>
        /// <param name="messageSet">The byte[] encode as a message set from kafka.</param>
        /// <returns>Enumerable representing stream of messages decoded from byte[]</returns>
        public static IEnumerable <Message> DecodeMessageSet(byte[] messageSet)
        {
            var stream = new ReadByteStream(messageSet);


            while (stream.HasData)
            {
                //if the message set hits against our max bytes wall on the fetch we will have a 1/2 completed message downloaded.
                //the decode should guard against this situation
                if (stream.Available(MinimumMessageSize) == false)
                {
                    yield break;
                }

                var offset      = stream.ReadLong();
                var messageSize = stream.ReadInt();

                if (stream.Available(messageSize) == false)
                {
                    yield break;
                }

                foreach (var message in DecodeMessage(offset, stream.ReadBytesFromStream(messageSize)))
                {
                    yield return(message);
                }
            }
        }