in src/Elastic.Transport/Components/Providers/RecyclableMemoryStream.cs [563:609]
public override void Write(byte[] buffer, int offset, int count)
{
CheckDisposed();
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
if (offset < 0)
throw new ArgumentOutOfRangeException(nameof(offset), offset,
"Offset must be in the range of 0 - buffer.Length-1");
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), count, "count must be non-negative");
if (count + offset > buffer.Length) throw new ArgumentException("count must be greater than buffer.Length - offset");
var blockSize = _memoryManager.BlockSize;
var end = (long)_position + count;
// Check for overflow
if (end > MaxStreamLength) throw new IOException("Maximum capacity exceeded");
EnsureCapacity((int)end);
if (_largeBuffer == null)
{
var bytesRemaining = count;
var bytesWritten = 0;
var blockAndOffset = GetBlockAndRelativeOffset(_position);
while (bytesRemaining > 0)
{
var currentBlock = _blocks[blockAndOffset.Block];
var remainingInBlock = blockSize - blockAndOffset.Offset;
var 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, _largeBuffer, _position, count);
_position = (int)end;
_length = Math.Max(_position, _length);
}