private void EnsureCapacity()

in src/RecyclableMemoryStream.cs [1530:1563]


        private void EnsureCapacity(long newCapacity)
        {
            if (newCapacity > this.memoryManager.MaximumStreamCapacity && this.memoryManager.MaximumStreamCapacity > 0)
            {
                this.memoryManager.ReportStreamOverCapacity(this.id, this.tag, newCapacity, this.AllocationStack);

                throw new OutOfMemoryException($"Requested capacity is too large: {newCapacity}. Limit is {this.memoryManager.MaximumStreamCapacity}.");
            }

            if (this.largeBuffer != null)
            {
                if (newCapacity > this.largeBuffer.Length)
                {
                    var newBuffer = this.memoryManager.GetLargeBuffer(newCapacity, this.id, this.tag);
                    Debug.Assert(this.length <= Int32.MaxValue);
                    this.InternalRead(newBuffer, 0, (int)this.length, 0);
                    this.ReleaseLargeBuffer();
                    this.largeBuffer = newBuffer;
                }
            }
            else
            {
                // Let's save some re-allocs of the blocks list
                var blocksRequired = (newCapacity / this.memoryManager.BlockSize) + 1;
                if (this.blocks.Capacity < blocksRequired)
                {
                    this.blocks.Capacity = (int)blocksRequired;
                }
                while (this.Capacity64 < newCapacity)
                {
                    this.blocks.Add((this.memoryManager.GetBlock()));
                }
            }
        }