in src/RecyclableMemoryStream.cs [979:1036]
public override void Write(byte[] buffer, int offset, int count)
{
this.CheckDisposed();
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (offset < 0)
{
throw new ArgumentOutOfRangeException(nameof(offset), offset,
$"{nameof(offset)} must be in the range of 0 - {nameof(buffer)}.{nameof(buffer.Length)}-1.");
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), count, $"{nameof(count)} must be non-negative.");
}
if (count + offset > buffer.Length)
{
throw new ArgumentException($"{nameof(count)} must be greater than {nameof(buffer)}.{nameof(buffer.Length)} - {nameof(offset)}.");
}
int blockSize = this.memoryManager.BlockSize;
long end = (long)this.position + count;
this.EnsureCapacity(end);
if (this.largeBuffer == null)
{
int bytesRemaining = count;
int bytesWritten = 0;
var blockAndOffset = this.GetBlockAndRelativeOffset(this.position);
while (bytesRemaining > 0)
{
byte[] currentBlock = this.blocks[blockAndOffset.Block];
int remainingInBlock = blockSize - blockAndOffset.Offset;
int amountToWriteInBlock = Math.Min(remainingInBlock, bytesRemaining);
Buffer.BlockCopy(buffer, offset + bytesWritten, currentBlock, blockAndOffset.Offset,
amountToWriteInBlock);
bytesRemaining -= amountToWriteInBlock;
bytesWritten += amountToWriteInBlock;
++blockAndOffset.Block;
blockAndOffset.Offset = 0;
}
}
else
{
Buffer.BlockCopy(buffer, offset, this.largeBuffer, (int)this.position, count);
}
this.position = end;
this.length = Math.Max(this.position, this.length);
}