本文整理匯總了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));
}
}
}