public static void ValidateValue(ref ReadOnlySpan <byte> value)
 {
     // TODO: Use throw helper with proper error messages
     if (value.Length > JsonConstants.MaxTokenSize)
     {
         JsonThrowHelper.ThrowArgumentException("Argument too large.");
     }
 }
 public static void ValidateProperty(ref ReadOnlySpan <char> propertyName)
 {
     // TODO: Use throw helper with proper error messages
     if (propertyName.Length > JsonConstants.MaxCharacterTokenSize)
     {
         JsonThrowHelper.ThrowArgumentException("Argument too large.");
     }
 }
 public static void ValidatePropertyAndValue(ref ReadOnlySpan <byte> propertyName, ref ReadOnlySpan <byte> value)
 {
     // TODO: Use throw helper with proper error messages
     if (propertyName.Length > JsonConstants.MaxTokenSize || value.Length > JsonConstants.MaxTokenSize)
     {
         JsonThrowHelper.ThrowArgumentException(propertyName, value);
     }
 }
        public Utf8JsonReaderStream(Stream jsonStream)
        {
            if (!jsonStream.CanRead || !jsonStream.CanSeek)
                JsonThrowHelper.ThrowArgumentException("Stream must be readable and seekable.");

            _pooledArray = ArrayPool<byte>.Shared.Rent(FirstSegmentSize);
            int numberOfBytes = jsonStream.Read(_pooledArray, 0, FirstSegmentSize);
            _buffer = _pooledArray.AsSpan(0, numberOfBytes);
            _stream = jsonStream;

            _isFinalBlock = numberOfBytes == 0;
            _jsonReader = new Utf8JsonReader(_buffer, _isFinalBlock);
            _consumed = 0;
        }
        private bool ReadNext()
        {
            bool result = false;

            do
            {
                _consumed += _jsonReader.BytesConsumed;
                int leftOver     = _buffer.Length - (int)_jsonReader.BytesConsumed;
                int amountToRead = StreamSegmentSize;
                if (leftOver > 0)
                {
                    _stream.Position -= leftOver;

                    if (_jsonReader.BytesConsumed == 0)
                    {
                        if (leftOver > 1_000_000_000)
                        {
                            JsonThrowHelper.ThrowArgumentException("Cannot fit left over data from the previous chunk and the next chunk of data into a 2 GB buffer.");
                        }

                        // This is guaranteed to not overflow due to the check above.
                        amountToRead += leftOver * 2;
                        ResizeBuffer(amountToRead);
                    }
                }

                if (_pooledArray.Length < StreamSegmentSize)
                {
                    ResizeBuffer(StreamSegmentSize);
                }

                int numberOfBytes = _stream.Read(_pooledArray, 0, amountToRead);
                _isFinalBlock = numberOfBytes == 0; // TODO: Can this be inferred differently based on leftOver and numberOfBytes

                _buffer = _pooledArray.AsSpan(0, numberOfBytes);

                _jsonReader = new JsonUtf8Reader(_buffer, _isFinalBlock, _jsonReader.CurrentState);
                result      = _jsonReader.Read();
            } while (!result && !_isFinalBlock);

            return(result);
        }