/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace MongoDB.Bson.IO
{
///
/// Represents a factory for IBsonBuffers.
///
public static class ByteBufferFactory
{
///
/// Creates a buffer of the specified length. Depending on the length, either a SingleChunkBuffer or a MultiChunkBuffer will be created.
///
/// The chunk pool.
/// The minimum capacity.
/// A buffer with at least the minimum capacity.
public static IByteBuffer Create(IBsonChunkSource chunkSource, int minimumCapacity)
{
if (chunkSource == null)
{
throw new ArgumentNullException("chunkSource");
}
if (minimumCapacity <= 0)
{
throw new ArgumentOutOfRangeException("minimumCapacity");
}
var capacity = 0;
var chunks = new List();
while (capacity < minimumCapacity)
{
var chunk = chunkSource.GetChunk(minimumCapacity - capacity);
chunks.Add(chunk);
capacity += chunk.Bytes.Count;
}
if (chunks.Count == 1)
{
var chunk = chunks[0];
ByteArrayChunk byteArrayChunk;
if ((byteArrayChunk = chunk as ByteArrayChunk) != null)
{
var segment = byteArrayChunk.Bytes;
if (segment.Offset == 0)
{
return new ByteArrayBuffer(segment.Array, segment.Count, isReadOnly: false);
}
}
return new SingleChunkBuffer(chunk, 0, isReadOnly: false);
}
else
{
return new MultiChunkBuffer(chunks, 0, isReadOnly: false);
}
}
}
}