/// <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);
            }
        }
Пример #2
0
        /// <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)
        {
            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;
            }
        }