private void LoadChunk(int n)
{
Contract.Requires(n > -1);
Contract.Requires(n <= _totalChunks);
if (n == _currentIndex) return;
if (n >= _totalChunks)
{
_currentChunk = CreateChunk(n);
_totalChunks = n + 1;
}
else
{
_currentChunk = _chunks.FindOne(new Doc {{"files_id", _fileInfo.Id}, {"n", n}});
}
_currentIndex = n;
var data = _currentChunk["data"] as byte[];
if (!IsFull(_currentChunk)) //non-full chunk
{
_buffer = new MemoryStream(new byte[_fileInfo.ChunkSize]);
_buffer.Write(data, 0, data.Length);
}
else
{
_buffer = new MemoryStream(data);
}
}