Beispiel #1
0
        public void LongLength()
        {
            using (var megabyte = MemoryPool <byte> .Shared.Rent(1024 * 1024))
            {
                var memory        = megabyte.Memory;
                var desiredLength = (1 + (int.MaxValue + 1L) / memory.Length) * memory.Length;
                desiredLength.ShouldBeGreaterThan(int.MaxValue);

                var sequence = CreateReadOnlySequence(memory, desiredLength);
                sequence.Length.ShouldBe(desiredLength);
                var e = Should.Throw <InvalidOperationException>(() => sequence.GetIntLength());
                e.Message.ShouldBe($@"You can't create arrays or string longer than int.MaxValue in .net. Packet length was: {desiredLength}.
See https://blogs.msdn.microsoft.com/joshwil/2005/08/10/bigarrayt-getting-around-the-2gb-array-size-limit/");
            }

            ReadOnlySequence <byte> CreateReadOnlySequence(Memory <byte> memory, long desiredLength)
            {
                ReadOnlySequenceSegment <byte> end     = null;
                ReadOnlySequenceSegment <byte> current = null;

                while (desiredLength > 0)
                {
                    desiredLength -= memory.Length;
                    var next = new LongSequenceSegment <byte>(memory, desiredLength, current);
                    current = next;
                    end     = end ?? next;
                }

                var sequence = new ReadOnlySequence <byte>(current, 0, end, end.Memory.Length);

                return(sequence);
            }
        }
        /// <summary>
        /// Cleans up any buffers rented for this reader
        /// </summary>
        public void Dispose()
        {
            ReadOnlySequenceSegment <byte>?Segment = _LeftoverHead;

            while (Segment != null)
            {
                if (MemoryMarshal.TryGetArray(Segment.Memory, out var MyBuffer))
                {
                    ArrayPool <byte> .Shared.Return(MyBuffer.Array !);
                }

                if (_HasExtraTail && Segment == _LeftoverTail)
                {
                    Segment = null;
                }
                else
                {
                    Segment = Segment.Next;
                }
            }

            _LeftoverHead = null;
            _LeftoverTail = null;
            _HasExtraTail = false;
            _LastBuffer   = default;

            if (_Dispose)
            {
                (_Reader as IDisposable)?.Dispose();
            }
        }
Beispiel #3
0
 /// <summary>
 /// Get <see cref="ReadOnlySequenceSegment{T}"/> from the underlying <see cref="ReadOnlySequence{T}"/>.
 /// If unable to get the <see cref="ReadOnlySequenceSegment{T}"/>, return false.
 /// </summary>
 public static bool TryGetReadOnlySequenceSegment <T>(ReadOnlySequence <T> sequence,
                                                      out ReadOnlySequenceSegment <T> startSegment,
                                                      out int startIndex,
                                                      out ReadOnlySequenceSegment <T> endSegment,
                                                      out int endIndex)
 {
     return(sequence.TryGetReadOnlySequenceSegment(out startSegment, out startIndex, out endSegment, out endIndex));
 }
Beispiel #4
0
 /// <summary>
 /// Get <see cref="ReadOnlySequenceSegment{T}"/> from the underlying <see cref="ReadOnlySequence{T}"/>.
 /// If unable to get the <see cref="ReadOnlySequenceSegment{T}"/>, return false.
 /// </summary>
 public static bool TryGetReadOnlySequenceSegment <T>(ReadOnlySequence <T> sequence,
                                                      out ReadOnlySequenceSegment <T>?startSegment,
                                                      out int startIndex,
                                                      [NotNullWhen(true)] out ReadOnlySequenceSegment <T>?endSegment,
                                                      out int endIndex)
 {
     return(sequence.TryGetReadOnlySequenceSegment(out startSegment, out startIndex, out endSegment, out endIndex));
 }
 /// <summary>
 /// Get <see cref="ReadOnlySequenceSegment{T}"/> from the underlying <see cref="ReadOnlySequence{T}"/>.
 /// If unable to get the <see cref="ReadOnlySequenceSegment{T}"/>, return false.
 /// </summary>
 public static bool TryGetReadOnlySequenceSegment <T>(ReadOnlySequence <T> sequence,
                                                      out ReadOnlySequenceSegment <T>?startSegment,
                                                      out int startIndex,
                                                      out ReadOnlySequenceSegment <T>?endSegment,
                                                      out int endIndex) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
 {
     return(sequence.TryGetReadOnlySequenceSegment(out startSegment, out startIndex, out endSegment, out endIndex));
 }
Beispiel #6
0
        private void GetNextSpan()
        {
            if (!Sequence.IsSingleSegment)
            {
                var next = _currentSegment.Next;
                if (next != null)
                {
                    _currentSegment = next;
                    CurrentSpan     = _currentSegment.Memory.Span;
                    CurrentSpanIdx  = 0;
                }
            }

            End = true;
        }
Beispiel #7
0
 private static Exception CreateArgumentValidationException <T>(ReadOnlySequenceSegment <T> startSegment, int startIndex, ReadOnlySequenceSegment <T> endSegment)
 {
     if (startSegment == null)
     {
         return(CreateArgumentNullException(ExceptionArgument.startSegment));
     }
     else if (endSegment == null)
     {
         return(CreateArgumentNullException(ExceptionArgument.endSegment));
     }
     else if ((uint)startSegment.Memory.Length < (uint)startIndex)
     {
         return(CreateArgumentOutOfRangeException(ExceptionArgument.startIndex));
     }
     else
     {
         return(CreateArgumentOutOfRangeException(ExceptionArgument.endIndex));
     }
 }
Beispiel #8
0
        public SequenceReader2(ReadOnlySequence <byte> sequence)
        {
            Sequence       = sequence;
            CurrentSpan    = sequence.First.Span;
            CurrentSpanIdx = 0;

            _position        = 0;
            _currentSegment  = sequence.Start.GetObject() as ReadOnlySequenceSegment <byte>;
            _startPosition   = sequence.Start;
            _currentPosition = _startPosition;

            End = CurrentSpan.Length == 0;

            if (End && !sequence.IsSingleSegment)
            {
                End = false;
                GetNextSpan();
            }
        }
Beispiel #9
0
        private void GetNextSpan2()
        {
            ReadOnlyMemory <byte> memory;

            if (!Sequence.IsSingleSegment)
            {
                while (Sequence.TryGet(ref _currentPosition, out memory, true))
                {
                }

                var next = _currentSegment.Next;
                if (next != null)
                {
                    _currentSegment = next;
                    CurrentSpan     = _currentSegment.Memory.Span;
                    CurrentSpanIdx  = 0;
                }
            }

            End = true;
        }
Beispiel #10
0
 internal TailSegment(ReadOnlySequenceSegment <byte> previous, Memory <byte> memory)
 {
     Memory       = memory;
     RunningIndex = previous.RunningIndex + previous.Memory.Length;
     SegmentSetter(previous, this);
 }
Beispiel #11
0
 public static void ThrowArgumentValidationException <T>(ReadOnlySequenceSegment <T>?startSegment, int startIndex, ReadOnlySequenceSegment <T>?endSegment)
 => throw CreateArgumentValidationException(startSegment, startIndex, endSegment);
 public void SetNext(ReadOnlySequenceSegment <byte> next)
 {
     Next = next;
 }
Beispiel #13
0
 public MyReadOnlySequenceSegment(ReadOnlyMemory <byte> memory, long runningIndex, ReadOnlySequenceSegment <byte> next)
 {
     this.Memory       = memory;
     this.RunningIndex = runningIndex;
     this.Next         = next;
 }
Beispiel #14
0
 public Segment(ReadOnlyMemory <T> memory, ReadOnlySequenceSegment <T> next, long runningIndex)
 {
     Memory       = memory;
     Next         = next;
     RunningIndex = runningIndex;
 }