/// <summary>
        /// Creates a buffer of the specified length. Depending on the length, either a SingleChunkBuffer or a MultiChunkBuffer will be created.
        /// </summary>
        /// <param name="chunkPool">The chunk pool.</param>
        /// <param name="length">The length.</param>
        /// <returns>A buffer.</returns>
        public static IByteBuffer Create(BsonChunkPool chunkPool, int length)
        {
            if (chunkPool == null)
            {
                throw new ArgumentNullException("pool");
            }
            if (length <= 0)
            {
                throw new ArgumentOutOfRangeException("length");
            }

            if (length < chunkPool.ChunkSize)
            {
                var chunk = chunkPool.AcquireChunk();
                return new SingleChunkBuffer(chunk, 0, length, false);
            }
            else
            {
                var chunksNeeded = ((length - 1) / chunkPool.ChunkSize) + 1;
                var chunks = new List<BsonChunk>(chunksNeeded);
                for (int i = 0; i < chunksNeeded; i++)
                {
                    chunks.Add(chunkPool.AcquireChunk());
                }
                return new MultiChunkBuffer(chunks, 0, length, false);
            }
        }
        /// <summary>
        /// Creates a buffer of the specified fixed capacity. Depending on the required capacity, either a SingleChunkBuffer or a MultiChunkBuffer will be created.
        /// </summary>
        /// <param name="chunkPool">The chunk pool.</param>
        /// <param name="fixedCapacity">The required capacity.</param>
        /// <returns>A buffer.</returns>
        public static IByteBuffer Create(BsonChunkPool chunkPool, int fixedCapacity)
        {
            if (chunkPool == null)
            {
                throw new ArgumentNullException("pool");
            }
            if (fixedCapacity <= 0)
            {
                throw new ArgumentOutOfRangeException("capacity");
            }

            if (fixedCapacity < chunkPool.ChunkSize)
            {
                var chunk = chunkPool.AcquireChunk();
                return(new SingleChunkBuffer(chunk, 0, 0, false));
            }
            else
            {
                var chunksNeeded = ((fixedCapacity - 1) / chunkPool.ChunkSize) + 1;
                var chunks       = new List <BsonChunk>(chunksNeeded);
                for (int i = 0; i < chunksNeeded; i++)
                {
                    chunks.Add(chunkPool.AcquireChunk());
                }
                return(new MultiChunkBuffer(chunks, 0, 0, false));
            }
        }
 private void ExpandCapacity(int targetCapacity)
 {
     while (_capacity < targetCapacity)
     {
         var chunk = _chunkPool.AcquireChunk();
         chunk.IncrementReferenceCount();
         _chunks.Add(chunk);
         _capacity += _chunkSize;
     }
 }
Esempio n. 4
0
        private void ExpandCapacity(int targetCapacity)
        {
            if (_chunkPool == null)
            {
                throw new InvalidOperationException("Capacity cannot be expanded because this buffer was created without specifying a chunk pool.");
            }

            while (_capacity < targetCapacity)
            {
                var chunk = _chunkPool.AcquireChunk();
                chunk.IncrementReferenceCount();
                _chunks.Add(chunk);
                _capacity += _chunkSize;
            }
        }