public static ReadOnlySequence <byte> Create(IEnumerable <Memory <byte> > buffers)
            {
                ReadOnlyBufferSegment segment = null;
                ReadOnlyBufferSegment first   = null;

                foreach (Memory <byte> buffer in buffers)
                {
                    var newSegment = new ReadOnlyBufferSegment()
                    {
                        Memory = buffer,
                    };

                    if (segment != null)
                    {
                        segment.Next            = newSegment;
                        newSegment.RunningIndex = segment.RunningIndex + segment.Memory.Length;
                    }
                    else
                    {
                        first = newSegment;
                    }

                    segment = newSegment;
                }

                if (first == null)
                {
                    first = segment = new ReadOnlyBufferSegment();
                }

                return(new ReadOnlySequence <byte>(first, 0, segment, segment.Memory.Length));
            }
Ejemplo n.º 2
0
        public ReadOnlyBuffer(IEnumerable <Memory <T> > buffers)
        {
            ReadOnlyBufferSegment segment = null;
            ReadOnlyBufferSegment first   = null;

            foreach (var buffer in buffers)
            {
                var newSegment = new ReadOnlyBufferSegment()
                {
                    Memory       = buffer,
                    RunningIndex = segment?.RunningIndex ?? 0
                };

                if (segment != null)
                {
                    segment.Next = newSegment;
                }
                else
                {
                    first = newSegment;
                }

                segment = newSegment;
            }

            if (first == null)
            {
                first = segment = new ReadOnlyBufferSegment();
            }

            BufferStart = new SequencePosition(first, 0 | MemoryListStartMask);
            BufferEnd   = new SequencePosition(segment, segment.Memory.Length | MemoryListEndMask);
        }
Ejemplo n.º 3
0
        public ReadOnlyBuffer(IEnumerable <Memory <byte> > buffers)
        {
            ReadOnlyBufferSegment segment = null;
            ReadOnlyBufferSegment first   = null;

            foreach (var buffer in buffers)
            {
                var newSegment = new ReadOnlyBufferSegment()
                {
                    Memory       = buffer,
                    VirtualIndex = segment?.VirtualIndex ?? 0
                };

                if (segment != null)
                {
                    segment.Next = newSegment;
                }
                else
                {
                    first = newSegment;
                }

                segment = newSegment;
            }

            if (first == null)
            {
                first = segment = new ReadOnlyBufferSegment();
            }

            BufferStart = new Position(first, 0);
            BufferEnd   = new Position(segment, segment.Memory.Length);
        }
        protected internal override ReadOnlySequence <byte> _GetSequence(int index, int count)
        {
            if (0u >= (uint)count)
            {
                return(ReadOnlySequence <byte> .Empty);
            }

            var c = FindComponent(index);

            if (c == FindComponent(index + count - 1))
            {
                return(c.GetSequence(index - c.Offset, count));
            }
            var array = ThreadLocalList <ReadOnlyMemory <byte> > .NewInstance(_nioBufferCount);

            try
            {
                int i          = c.Index;
                int adjustment = c.Offset;
                var s          = c.Buf;
                for (; ;)
                {
                    int localLength = Math.Min(count, s.ReadableBytes - (index - adjustment));
                    switch (s.IoBufferCount)
                    {
                    case 0:
                        ThrowHelper.ThrowNotSupportedException();
                        break;

                    case 1:
                        array.Add(s.GetReadableMemory(index - adjustment, localLength));
                        break;

                    default:
                        var sequence = s.GetSequence(index - adjustment, localLength);
                        foreach (var memory in sequence)
                        {
                            array.Add(memory);
                        }
                        break;
                    }

                    index      += localLength;
                    count      -= localLength;
                    adjustment += s.ReadableBytes;
                    if ((uint)(count - 1) > SharedConstants.TooBigOrNegative) // count <= 0
                    {
                        break;
                    }
                    s = Buffer(++i);
                }

                return(ReadOnlyBufferSegment.Create(array));
            }
            finally
            {
                array.Return();
            }
        }
        protected internal override ReadOnlySequence <byte> _GetSequence(int index, int count)
        {
            if (0u >= (uint)count)
            {
                return(ReadOnlySequence <byte> .Empty);
            }

            var buffers = ThreadLocalList <ReadOnlyMemory <byte> > .NewInstance(_componentCount);

            try
            {
                int i = ToComponentIndex0(index);
                while (count > 0)
                {
                    ComponentEntry c           = _components[i];
                    IByteBuffer    s           = c.Buffer;
                    int            localLength = Math.Min(count, c.EndOffset - index);
                    switch (s.IoBufferCount)
                    {
                    case 0:
                        ThrowHelper.ThrowNotSupportedException();
                        break;

                    case 1:
                        if (s.IsSingleIoBuffer)
                        {
                            buffers.Add(s.GetReadableMemory(c.Idx(index), localLength));
                        }
                        else
                        {
                            var sequence0 = s.GetSequence(c.Idx(index), localLength);
                            foreach (var memory in sequence0)
                            {
                                buffers.Add(memory);
                            }
                        }
                        break;

                    default:
                        var sequence = s.GetSequence(c.Idx(index), localLength);
                        foreach (var memory in sequence)
                        {
                            buffers.Add(memory);
                        }
                        break;
                    }

                    index += localLength;
                    count -= localLength;
                    i++;
                }

                return(ReadOnlyBufferSegment.Create(buffers));
            }
            finally
            {
                buffers.Return();
            }
        }
Ejemplo n.º 6
0
        public ReadOnlyBuffer(Memory <T> data)
        {
            var segment = new ReadOnlyBufferSegment
            {
                Memory = data
            };

            BufferStart = new SequencePosition(segment, 0 | MemoryListStartMask);
            BufferEnd   = new SequencePosition(segment, data.Length | MemoryListEndMask);
        }
Ejemplo n.º 7
0
        public ReadOnlyBuffer(Memory <T> data)
        {
            var segment = new ReadOnlyBufferSegment
            {
                Memory = data
            };

            BufferStart = new Position(segment, 0);
            BufferEnd   = new Position(segment, data.Length);
        }
Ejemplo n.º 8
0
        private static ReadOnlySequence <byte> CreateReadOnlySequence(params byte[][] buffers)
        {
            if (buffers.Length == 1)
            {
                return(new ReadOnlySequence <byte>(buffers[0]));
            }
            var list = new List <Memory <byte> >();

            foreach (var buffer in buffers)
            {
                list.Add(buffer);
            }
            return(ReadOnlyBufferSegment.Create(list.ToArray()));
        }
Ejemplo n.º 9
0
 public static ReadOnlySequence <byte> ToReadOnlySequence(this IEnumerable <Memory <byte> > buffers) => ReadOnlyBufferSegment.Create(buffers);
Ejemplo n.º 10
0
 public static ReadOnlySequence <byte> Create(IEnumerable <Memory <byte> > buffers)
 {
     return(ReadOnlyBufferSegment.Create(buffers));
 }