Exemplo n.º 1
0
 /// <summary>
 /// Writes a new buffer into the pipeline. The task returned by this operation only completes when the next
 /// Read has been queued, or the Reader has completed, since the buffer provided here needs to be kept alive
 /// until the matching Read finishes (because we don't have ownership tracking when working with unowned buffers)
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 public async Task WriteAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken)
 {
     using (var unowned = new UnownedBuffer(buffer))
     {
         await WriteAsync(unowned, cancellationToken);
     }
 }
Exemplo n.º 2
0
        public static ReadableBuffer CreateBuffer(params byte[][] inputs)
        {
            if (inputs == null || inputs.Length == 0)
            {
                throw new InvalidOperationException();
            }

            var i = 0;

            BufferSegment last  = null;
            BufferSegment first = null;

            do
            {
                var s            = inputs[i];
                var length       = s.Length;
                var memoryOffset = length;
                var dataOffset   = length * 2;
                var chars        = new byte[length * 8];

                for (int j = 0; j < length; j++)
                {
                    chars[dataOffset + j] = s[j];
                }

                // Create a segment that has offset relative to the OwnedBuffer and OwnedBuffer itself has offset relative to array
                var ownedBuffer = new UnownedBuffer(new ArraySegment <byte>(chars, memoryOffset, length * 3));
                var current     = new BufferSegment(ownedBuffer, length, length * 2);
                if (first == null)
                {
                    first = current;
                    last  = current;
                }
                else
                {
                    last.Next = current;
                    last      = current;
                }
                i++;
            } while (i < inputs.Length);

            return(new ReadableBuffer(new ReadCursor(first, first.Start), new ReadCursor(last, last.Start + last.ReadableBytes)));
        }