public void Discard() { _buffer.Discard(); var readResult = _buffer.Read(); if (!readResult.IsEmpty) { _buffer.ConfirmRead((int)readResult.Length); } _currentCompression = CompressionAlgorithm.None; }
public void FlushDiscardWithSegmentOverflow() { const int segmentSize = 13; var source = Enumerable.Range(0, 1000).Select(n => unchecked ((byte)n)).ToArray(); var target = new byte[source.Length]; var buffer = new ReadWriteBuffer(segmentSize); for (int i = 0; i < 100; i++) { var rnd = new Random(i); Array.Clear(target, 0, target.Length); int sourcePosition = 0; int targetPosition = 0; int discardPosition = 0; int readWriteFactor = i % 3 + 2; while (discardPosition != source.Length) { var read = rnd.Next(readWriteFactor) == 0; if (read) { var availableBytes = buffer.Read(); if (!availableBytes.IsEmpty) { var availableLen = Math.Min((int)availableBytes.Length, Math.Min(rnd.Next(segmentSize * 3) + 1, target.Length - targetPosition)); var targetSlice = new Span <byte>(target).Slice(targetPosition, availableLen); availableBytes.Slice(0, availableLen).CopyTo(targetSlice); targetPosition += availableLen; buffer.ConfirmRead(availableLen); continue; } } var len = Math.Min(source.Length - sourcePosition, rnd.Next(segmentSize * 3) + 1); var memory = buffer.GetMemory(len); new ReadOnlySpan <byte>(source).Slice(sourcePosition, len).CopyTo(memory.Span.Slice(0, len)); buffer.ConfirmWrite(len); sourcePosition += len; if (rnd.Next(4) == 0) { buffer.Flush(); discardPosition = sourcePosition; } else if (rnd.Next(2) == 0) { buffer.Discard(); sourcePosition = discardPosition; } } var lastBlock = buffer.Read(); Assert.Equal(target.Length - targetPosition, lastBlock.Length); lastBlock.CopyTo(new Span <byte>(target, targetPosition, target.Length - targetPosition)); Assert.Equal(source, target); buffer.ConfirmRead((int)lastBlock.Length); } }