in src/RecyclableMemoryStream.cs [1371:1418]
public void WriteTo(byte[] buffer, long offset, long count, int targetOffset)
{
this.CheckDisposed();
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
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 stream.",
innerException: null);
}
if (targetOffset < 0 || count + targetOffset > buffer.Length)
{
throw new ArgumentOutOfRangeException(
message: $"{nameof(targetOffset)} must not be negative and {nameof(targetOffset)} + {nameof(count)} must not exceed the length of the target {nameof(buffer)}.",
innerException: null);
}
if (this.largeBuffer == null)
{
var blockAndOffset = GetBlockAndRelativeOffset(offset);
long bytesRemaining = count;
int currentBlock = blockAndOffset.Block;
int currentOffset = blockAndOffset.Offset;
int currentTargetOffset = targetOffset;
while (bytesRemaining > 0)
{
int amountToCopy = (int)Math.Min((long)this.blocks[currentBlock].Length - currentOffset, bytesRemaining);
Buffer.BlockCopy(this.blocks[currentBlock], currentOffset, buffer, currentTargetOffset, amountToCopy);
bytesRemaining -= amountToCopy;
++currentBlock;
currentOffset = 0;
currentTargetOffset += amountToCopy;
}
}
else
{
AssertLengthIsSmall();
Buffer.BlockCopy(this.largeBuffer, (int)offset, buffer, targetOffset, (int)count);
}
}