in src/RecyclableMemoryStream.cs [1289:1326]
public void WriteTo(Stream stream, long offset, long count)
{
this.CheckDisposed();
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
if (offset < 0 || offset + count > this.length)
{
throw new ArgumentOutOfRangeException(
message: $"{nameof(offset)} must not be negative and {nameof(offset)} + {nameof(count)} must not exceed the length of the {nameof(stream)}.",
innerException: null);
}
if (this.largeBuffer == null)
{
var blockAndOffset = this.GetBlockAndRelativeOffset(offset);
long bytesRemaining = count;
int currentBlock = blockAndOffset.Block;
int currentOffset = blockAndOffset.Offset;
while (bytesRemaining > 0)
{
int amountToCopy = (int)Math.Min((long)this.blocks[currentBlock].Length - currentOffset, bytesRemaining);
stream.Write(this.blocks[currentBlock], currentOffset, amountToCopy);
bytesRemaining -= amountToCopy;
++currentBlock;
currentOffset = 0;
}
}
else
{
stream.Write(this.largeBuffer, (int)offset, (int)count);
}
}