當前位置: 首頁>>代碼示例>>C#>>正文


C# BigEndianBinaryReader.ReadIntPrefixedBytes方法代碼示例

本文整理匯總了C#中BigEndianBinaryReader.ReadIntPrefixedBytes方法的典型用法代碼示例。如果您正苦於以下問題:C# BigEndianBinaryReader.ReadIntPrefixedBytes方法的具體用法?C# BigEndianBinaryReader.ReadIntPrefixedBytes怎麽用?C# BigEndianBinaryReader.ReadIntPrefixedBytes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BigEndianBinaryReader的用法示例。


在下文中一共展示了BigEndianBinaryReader.ReadIntPrefixedBytes方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: DecodeMessage

        /// <summary>
        /// Decode messages from a payload and assign it a given kafka offset.
        /// </summary>
        /// <param name="offset">The offset represting the log entry from kafka of this message.</param>
        /// <param name="payload">The byte[] encode as a message from kafka.</param>
        /// <returns>Enumerable representing stream of messages decoded from byte[].</returns>
        /// <remarks>The return type is an Enumerable as the message could be a compressed message set.</remarks>
        public static IEnumerable<Message> DecodeMessage(long offset, byte[] payload)
        {
            var crc = payload.Take(4).ToArray();
            using (var stream = new BigEndianBinaryReader(payload.Skip(4)))
            {
                if (crc.SequenceEqual(stream.CrcHash()) == false)
                    throw new FailCrcCheckException("Buffer did not match CRC validation.");

                var message = new Message
                {
                    Meta = new MessageMetadata { Offset = offset },
                    MagicNumber = stream.ReadByte(),
                    Attribute = stream.ReadByte(),
                    Key = stream.ReadIntPrefixedBytes()
                };

                var codec = (MessageCodec)(ProtocolConstants.AttributeCodeMask & message.Attribute);
                switch (codec)
                {
                    case MessageCodec.CodecNone:
                        message.Value = stream.ReadIntPrefixedBytes();
                        yield return message;
                        break;
                    case MessageCodec.CodecGzip:
                        var gZipData = stream.ReadIntPrefixedBytes();
                        foreach (var m in DecodeMessageSet(Compression.Unzip(gZipData)))
                        {
                            yield return m;
                        }
                        break;
                    default:
                        throw new NotSupportedException(string.Format("Codec type of {0} is not supported.", codec));
                }
            }
        }
開發者ID:jsifantu,項目名稱:kafka-net,代碼行數:42,代碼來源:Message.cs


注:本文中的BigEndianBinaryReader.ReadIntPrefixedBytes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。