public Stream IncreaseSize(int value, out long offset)
{
lock (_lock)
{
while (Interlocked.Read(ref _counter) != 0)
{
if (!Monitor.Wait(_lock, 10000, true))
throw new NotSupportedException();
}
FileStream file = new FileStream(_filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
try
{
offset = MathEx.RoundUp(file.Length, 0x800);
file.SetLength(offset + value);
_mmf = MemoryMappedFile.CreateFromFile(file, null, 0, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.Inheritable, false);
}
catch
{
file.SafeDispose();
throw;
}
Interlocked.Increment(ref _counter);
DisposableStream result = new DisposableStream(_mmf.CreateViewStream(offset, value, MemoryMappedFileAccess.ReadWrite));
result.AfterDispose.Add(new DisposableAction(Free));
return result;
}
}