public override int Read(byte[] buffer, int offset, int count)
{
if (_disposed) throw new ObjectDisposedException(this.GetType().FullName);
if (offset < 0 || buffer.Length < offset) throw new ArgumentOutOfRangeException(nameof(offset));
if (count < 0 || (buffer.Length - offset) < count) throw new ArgumentOutOfRangeException(nameof(count));
if (count == 0) return 0;
if (_blockInfo.IsUpdated)
{
this.Flush();
}
count = (int)Math.Min(count, _length - _position);
int readSumLength = 0;
while (count > 0)
{
long p = (_position / SectorSize) * SectorSize;
if (_blockInfo.Position != p)
{
_blockInfo.Position = p;
_blockInfo.Offset = 0;
_stream.Seek(_blockInfo.Position, SeekOrigin.Begin);
var readLength = _stream.Read(_blockInfo.Value, 0, SectorSize);
readLength = (int)Math.Min(_length - _blockInfo.Position, readLength);
_blockInfo.Count = readLength;
}
int blockReadPosition = (int)(_position - p);
int length = Math.Min(SectorSize - blockReadPosition, count);
Unsafe.Copy(_blockInfo.Value, blockReadPosition, buffer, offset, length);
offset += length;
count -= length;
_position += length;
readSumLength += length;
}
return readSumLength;
}