示例#1
0
    async Task Write(byte[] buffer, int offset, int count, bool async, CancellationToken cancellationToken = default)
    {
        if (buffer == null)
        {
            throw new ArgumentNullException(nameof(buffer));
        }
        if (offset < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(offset));
        }
        if (count < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(count));
        }
        if (buffer.Length - offset < count)
        {
            throw new ArgumentException("Invalid offset or count for this buffer");
        }

        CheckDisposed();

        if (!_writeable)
        {
            throw new NotSupportedException("Write cannot be called on a stream opened with no write permissions");
        }

        var totalWritten = 0;

        while (totalWritten < count)
        {
            var chunkSize    = Math.Min(count - totalWritten, _manager.MaxTransferBlockSize);
            var bytesWritten = await _manager.ExecuteFunction <int>("lowrite", async, cancellationToken, _fd, new ArraySegment <byte>(buffer, offset + totalWritten, chunkSize));

            totalWritten += bytesWritten;

            if (bytesWritten != chunkSize)
            {
                throw new InvalidOperationException($"Internal Npgsql bug, please report");
            }

            _pos += bytesWritten;
        }
    }