public void DisposeResetsTheMemoryBufferWriter() { var bufferWriter = new MemoryBufferWriter(); bufferWriter.WriteByte(1); Assert.Equal(1, bufferWriter.Length); bufferWriter.Dispose(); Assert.Equal(0, bufferWriter.Length); }
public void WriteByteWorksAsFirstCall() { using (var bufferWriter = new MemoryBufferWriter()) { bufferWriter.WriteByte(234); var data = bufferWriter.ToArray(); Assert.Equal(1, bufferWriter.Length); Assert.Single(data); Assert.Equal(234, data[0]); } }
public void WriteByteWorksAsFirstCall_CopyTo() { using (var bufferWriter = new MemoryBufferWriter()) { bufferWriter.WriteByte(234); Assert.Equal(1, bufferWriter.Length); var data = new byte[bufferWriter.Length]; bufferWriter.CopyTo(data); Assert.Equal(234, data[0]); } }
public void WriteByteWorksIfFirstByteInNewSegment() { var inputSize = MinimumSegmentSize; var input = Enumerable.Range(0, inputSize).Select(i => (byte)i).ToArray(); using (var bufferWriter = new MemoryBufferWriter(MinimumSegmentSize)) { bufferWriter.Write(input, 0, input.Length); Assert.Equal(16, bufferWriter.Length); bufferWriter.WriteByte(16); Assert.Equal(17, bufferWriter.Length); var data = bufferWriter.ToArray(); Assert.Equal(input, data.Take(16)); Assert.Equal(16, data[16]); } }
public void WriteSpanWorksAtNonZeroOffset_CopyTo() { using (var bufferWriter = new MemoryBufferWriter()) { bufferWriter.WriteByte(1); bufferWriter.Write(new byte[] { 2, 3, 4 }.AsSpan()); Assert.Equal(4, bufferWriter.Length); var data = new byte[bufferWriter.Length]; bufferWriter.CopyTo(data); Assert.Equal(1, data[0]); Assert.Equal(2, data[1]); Assert.Equal(3, data[2]); Assert.Equal(4, data[3]); } }
public void WriteByteWorksIfSegmentHasSpace() { var input = new byte[] { 11, 12, 13 }; using (var bufferWriter = new MemoryBufferWriter()) { bufferWriter.Write(input, 0, input.Length); bufferWriter.WriteByte(14); Assert.Equal(4, bufferWriter.Length); var data = bufferWriter.ToArray(); Assert.Equal(4, data.Length); Assert.Equal(11, data[0]); Assert.Equal(12, data[1]); Assert.Equal(13, data[2]); Assert.Equal(14, data[3]); } }
public void WriteRecord(Message <TKey, TValue> message, IBufferWriter <byte> output) { // Record... // length: varint // attributes: int8 // bit 0~7: unused // timestampDelta: varint // offsetDelta: varint // keyLength: varint // key: byte[] // valueLen: varint // value: byte[] // Headers => [Header] // Header... // headerKeyLength: varint // headerKey: String // headerValueLength: varint // Value: byte[] using var buffer = new MemoryBufferWriter(); buffer.WriteByte(0); // attributes: int8 - bit 0~7: unused buffer.WriteVarInt((uint)0); // timestampDelta: varint buffer.WriteVarInt((uint)0); // offsetDelta: varint buffer.WritePrefixed(keySerializer, message.Key, BufferWriterExtensions.PrefixType.VarInt); buffer.WritePrefixed(valueSerializer, message.Value, BufferWriterExtensions.PrefixType.VarInt); // headers buffer.WriteVarInt((ulong)message.HeadersCount); message.ForEachHeader(h => { buffer.WriteCompactString(h.Key); buffer.WritePrefixed(h.Value.AsSpan(), BufferWriterExtensions.PrefixType.VarInt); }); output.WriteVarInt(buffer.Length); buffer.CopyTo(output); }