Library.Io.UnbufferedFileStream.Flush C# (CSharp) Method

Flush() public method

public Flush ( ) : void
return void
        public override void Flush()
        {
            if (_disposed) throw new ObjectDisposedException(this.GetType().FullName);

            if (_blockInfo.Position == -1) return;

            if (_blockInfo.IsUpdated)
            {
                _length = Math.Max(_length, _blockInfo.Position + _blockInfo.Offset + _blockInfo.Count);

                if (_blockInfo.Offset != 0 || _blockInfo.Count != SectorSize)
                {
                    using (var safeBuffer = _bufferManager.CreateSafeBuffer(SectorSize))
                    {
                        _stream.Seek(_blockInfo.Position, SeekOrigin.Begin);

                        var readLength = _stream.Read(safeBuffer.Value, 0, SectorSize);
                        readLength = (int)Math.Min(_length - _blockInfo.Position, readLength);

                        Unsafe.Zero(safeBuffer.Value, readLength, SectorSize - readLength);
                        Unsafe.Copy(_blockInfo.Value, _blockInfo.Offset, safeBuffer.Value, _blockInfo.Offset, _blockInfo.Count);

                        _stream.Seek(_blockInfo.Position, SeekOrigin.Begin);
                        _stream.Write(safeBuffer.Value, 0, SectorSize);
                    }
                }
                else
                {
                    _stream.Seek(_blockInfo.Position, SeekOrigin.Begin);
                    _stream.Write(_blockInfo.Value, 0, _blockInfo.Count);
                }
            }

            _blockInfo.IsUpdated = false;
            _blockInfo.Position = -1;
            _blockInfo.Offset = 0;
            _blockInfo.Count = 0;

            _stream.Flush();
        }