Exemple #1
0
        /// <inheritdoc />
        public async Task Write(ArraySegment <byte> value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, [CanBeNull] NpgsqlParameter parameter, bool async)
        {
            if (value.Array is null)
            {
                return;
            }

            if (!(parameter == null || parameter.Size <= 0 || parameter.Size >= value.Count))
            {
                value = new ArraySegment <byte>(value.Array, value.Offset, Math.Min(parameter.Size, value.Count));
            }

            // The entire segment fits in our buffer, copy it as usual.
            if (value.Count <= buf.WriteSpaceLeft)
            {
                buf.WriteBytes(value.Array, value.Offset, value.Count);
                return;
            }

            // The segment is larger than our buffer. Flush whatever is currently in the buffer and
            // write the array directly to the socket.
            await buf.Flush(async);

            buf.DirectWrite(value.Array, value.Offset, value.Count);
        }
Exemple #2
0
        async Task Write(byte[] value, NpgsqlWriteBuffer buf, int offset, int count, bool async)
        {
            // The entire segment fits in our buffer, copy it as usual.
            if (count <= buf.WriteSpaceLeft)
            {
                buf.WriteBytes(value, offset, count);
                return;
            }

            // The segment is larger than our buffer. Flush whatever is currently in the buffer and
            // write the array directly to the socket.
            await buf.Flush(async);

            await buf.DirectWrite(value, offset, count, async);
        }
Exemple #3
0
        async Task Write(byte[] value, NpgsqlWriteBuffer buf, int offset, int count, bool async, CancellationToken cancellationToken = default)
        {
            // The entire segment fits in our buffer, copy it as usual.
            if (count <= buf.WriteSpaceLeft)
            {
                buf.WriteBytes(value, offset, count);
                return;
            }

            // The segment is larger than our buffer. Flush whatever is currently in the buffer and
            // write the array directly to the socket.
            await buf.Flush(async, cancellationToken);

            await buf.DirectWrite(new ReadOnlyMemory <byte>(value, offset, count), async, cancellationToken);
        }
Exemple #4
0
        /// <inheritdoc />
        public async Task Write(ReadOnlyMemory <byte> value, NpgsqlWriteBuffer buf, NpgsqlLengthCache?lengthCache, NpgsqlParameter?parameter, bool async)
        {
            if (parameter != null && parameter.Size > 0 && parameter.Size < value.Length)
            {
                value = value.Slice(0, parameter.Size);
            }

            // The entire segment fits in our buffer, copy it into the buffer as usual.
            if (value.Length <= buf.WriteSpaceLeft)
            {
                buf.WriteBytes(value.Span);
                return;
            }

            // The segment is larger than our buffer. Perform a direct write, flushing whatever is currently in the buffer
            // and then writing the array directly to the socket.
            await buf.DirectWrite(value, async);
        }
Exemple #5
0
        /// <inheritdoc />
        public override async Task Write(byte[] value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, [CanBeNull] NpgsqlParameter parameter, bool async)
        {
            var len = parameter == null || parameter.Size <= 0 || parameter.Size >= value.Length
                ? value.Length
                : parameter.Size;

            // The entire array fits in our buffer, copy it into the buffer as usual.
            if (len <= buf.WriteSpaceLeft)
            {
                buf.WriteBytes(value, 0, len);
                return;
            }

            // The segment is larger than our buffer. Flush whatever is currently in the buffer and
            // write the array directly to the socket.
            await buf.Flush(async);

            buf.DirectWrite(value, 0, len);
        }
Exemple #6
0
        internal override async Task Write(NpgsqlWriteBuffer buf, bool async)
        {
            if (buf.WriteSpaceLeft < 1 + 5)
            {
                await buf.Flush(async);
            }
            buf.WriteByte(Code);
            buf.WriteInt32(4 + PayloadLength);

            if (PayloadLength <= buf.WriteSpaceLeft)
            {
                // The entire array fits in our buffer, copy it into the buffer as usual.
                buf.WriteBytes(Payload, PayloadOffset, Payload.Length);
                return;
            }

            await buf.Flush(async);

            buf.DirectWrite(Payload, PayloadOffset, PayloadLength);
        }