/// <summary> /// Returns a sequence containing the contents of the stream. /// </summary> /// <returns>A ReadOnlySequence of bytes</returns> /// <remarks>IMPORTANT: Doing a Write(), Dispose(), or Close() after calling GetReadOnlySequence() invalidates the sequence.</remarks> /// <exception cref="ObjectDisposedException">Object has been disposed</exception> public ReadOnlySequence <byte> GetReadOnlySequence() { this.CheckDisposed(); if (this.largeBuffer != null) { return(new ReadOnlySequence <byte>(this.largeBuffer, 0, length)); } if (this.blocks.Count == 1) { return(new ReadOnlySequence <byte>(this.blocks[0], 0, length)); } BlockSegment first = new BlockSegment(this.blocks[0]); BlockSegment last = first; for (int blockIdx = 1; blockIdx < blocks.Count; blockIdx++) { last = last.Append(this.blocks[blockIdx]); } return(new ReadOnlySequence <byte>(first, 0, last, this.length - (int)last.RunningIndex)); }
public BlockSegment Append(Memory <byte> memory) { var nextSegment = new BlockSegment(memory) { RunningIndex = RunningIndex + Memory.Length }; Next = nextSegment; return(nextSegment); }