Example #1
0
        private void AdjustBufferSize()
        {
            if (timeInSize++ < steps)
            {
                return;
            }

            if (internalBuffer.Length >= MaxBufferSize)
            {
                return;
            }

            timeInSize = 0;
            steps     += 2;
            var newSize = Math.Min(internalBuffer.Length * 2, MaxBufferSize);

            bufferPool.ReturnBuffer(internalBuffer);
            internalBuffer = bufferPool.TakeBuffer(newSize);
        }
        private void EnsureCapacity(long requestedCapacity)
        {
            if (requestedCapacity <= _buffer.Length)
            {
                return;
            }

            //estimate that the needed buffer growth is at most twice the old length
            var estimatedNewCapacity = _buffer.Length * 2;

            //precaution -> to make sure casting long to int is ok (I doubt this will ever be not ok, but still)
            Debug.Assert(requestedCapacity <= Int32.MaxValue, "should never grow buffer to these sizes");

            //if the required capacity is more than the estimated growth, grow the buffer by the requested capacity
            var newCapacity = (requestedCapacity <= estimatedNewCapacity) ? estimatedNewCapacity : (int)requestedCapacity;
            var newBuffer   = _bufferPool.TakeBuffer(newCapacity);

            Buffer.BlockCopy(_buffer, 0, newBuffer, 0, _position);
            _bufferPool.ReturnBuffer(_buffer);
            _buffer = newBuffer;
        }
 public BufferPoolMemoryStream(IBufferPool bufferPool)
 {
     _bufferPool = bufferPool;
     _buffer     = _bufferPool.TakeBuffer(8 * 1024);
 }
 public BufferPoolMemoryStream(IBufferPool bufferPool)
 {
     _bufferPool = bufferPool;
     _buffer = _bufferPool.TakeBuffer(8 * 1024);
 }