public void Read(Stream source) { var size = source.ReadInt32(); if (size == 0) { return; } var data = ArrayPool <byte> .Shared.Rent(size); try { source.Read(data, 0, size); using var tmp = new MemoryStream(data, 0, size); this.BaseOffset = tmp.ReadInt64(); this.BatchLength = tmp.ReadInt32(); this.PartitionLeaderEpoch = tmp.ReadInt32(); this.Magic = (byte)tmp.ReadByte(); this.Crc = tmp.ReadInt32(); var crc = (int)Crc32CHash.Compute( data, 8 + 4 + 4 + 1 + 4, this.BatchLength - 4 - 1 - 4); if (crc != this.Crc) { throw new Exception("Corrupt message"); } this.Attributes = tmp.ReadInt16(); this.LastOffsetDelta = tmp.ReadInt32(); this.FirstTimestamp = tmp.ReadInt64(); this.MaxTimestamp = tmp.ReadInt64(); this.ProducerId = tmp.ReadInt64(); this.ProducerEpoch = tmp.ReadInt16(); this.BaseSequence = tmp.ReadInt32(); this.Records = tmp.ReadArray <Record>(); } finally { ArrayPool <byte> .Shared.Return(data); } }
public void Write(Stream destination) { using var crcSlice = MemoryStreamFactory.GetStream(); crcSlice.WriteInt16(this.Attributes); crcSlice.WriteInt32(this.LastOffsetDelta); crcSlice.WriteInt64(this.FirstTimestamp); crcSlice.WriteInt64(this.MaxTimestamp); crcSlice.WriteInt64(this.ProducerId); crcSlice.WriteInt16(this.ProducerEpoch); crcSlice.WriteInt32(this.BaseSequence); crcSlice.WriteArray(this.Records); var crcSliceLength = (int)crcSlice.Length; this.Crc = (int)Crc32CHash.Compute(crcSlice.GetBuffer(), 0, crcSliceLength); destination.WriteInt32(crcSliceLength + 8 + 4 + 4 + 1 + 4); destination.WriteInt64(this.BaseOffset); destination.WriteInt32(this.BatchLength = GetBatchSizeFromCrcSliceSize(crcSliceLength)); destination.WriteInt32(this.PartitionLeaderEpoch); destination.WriteByte(this.Magic); destination.WriteInt32(this.Crc); crcSlice.WriteTo(destination); }