Exemplo n.º 1
0
        /// <summary>
        /// Writes the source <see cref="Span{Byte}"/> to the <see cref="WritableBuffer"/>.
        /// </summary>
        /// <param name="buffer">The <see cref="WritableBuffer"/></param>
        /// <param name="source">The <see cref="Span{Byte}"/> to write</param>
        public static void Write(this WritableBuffer buffer, ReadOnlySpan <byte> source)
        {
            if (buffer.Buffer.IsEmpty)
            {
                buffer.Ensure();
            }

            // Fast path, try copying to the available memory directly
            if (source.Length <= buffer.Buffer.Length)
            {
                source.CopyTo(buffer.Buffer.Span);
                buffer.Advance(source.Length);
                return;
            }

            var remaining = source.Length;
            var offset    = 0;

            while (remaining > 0)
            {
                var writable = Math.Min(remaining, buffer.Buffer.Length);

                buffer.Ensure(writable);

                if (writable == 0)
                {
                    continue;
                }

                source.Slice(offset, writable).CopyTo(buffer.Buffer.Span);

                remaining -= writable;
                offset    += writable;

                buffer.Advance(writable);
            }
        }
 public void Advance(int count)
 {
     _span = _span.Slice(count);
     _writableBuffer.Advance(count);
 }