Esempio n. 1
0
        /// <summary>
        /// Parses the next <see cref="BList"/> from the stream.
        /// </summary>
        /// <param name="stream">The stream to parse from.</param>
        /// <returns>The parsed <see cref="BList"/>.</returns>
        /// <exception cref="InvalidBencodeException{BList}">Invalid bencode</exception>
        protected BList ParseList(BencodeStream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (stream.Length < MinimumListLength)
            {
                throw InvalidBencodeException <BList> .BelowMinimumLength(MinimumListLength, stream.Length, stream.Position);
            }

            // Lists must start with 'l'
            char c = stream.ReadChar();

            if (c != 'l')
            {
                throw InvalidBencodeException <BList> .UnexpectedChar('l', c, stream.Position);
            }

            var list = new BList();

            // Loop until next character is the end character 'e' or end of stream
            while (stream.Peek() != 'e' && stream.Peek() != -1)
            {
                // Decode next object in stream
                var bObject = Parse(stream);
                list.Add(bObject);
            }

            c = stream.ReadChar();
            if (c != 'e')
            {
                if (stream.EndOfStream)
                {
                    throw InvalidBencodeException <BList> .MissingEndChar();
                }
                throw InvalidBencodeException <BList> .InvalidEndChar(c, stream.Position);
            }

            return(list);
        }