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

Write() public method

public Write ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
return void
        public override void Write(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;

            while (count > 0)
            {
                long p = (_position / SectorSize) * SectorSize;

                if (_blockInfo.Position != p)
                {
                    this.Flush();
                }

                _stream.Seek(p, SeekOrigin.Begin);

                int blockWritePosition = (int)(_position - p);
                int length = Math.Min(SectorSize - blockWritePosition, count);

                _blockInfo.Position = p;
                Unsafe.Copy(buffer, offset, _blockInfo.Value, blockWritePosition, length);
                if (_blockInfo.Count == 0) _blockInfo.Offset = blockWritePosition;
                _blockInfo.Count = (length + blockWritePosition) - _blockInfo.Offset;

                _blockInfo.IsUpdated = true;

                offset += length;
                count -= length;

                _position += length;
            }
        }