Exemple #1
0
        /// <summary>
        /// Decodes a BEncodedString from the supplied StreamReader
        /// </summary>
        /// <param name="reader">The StreamReader containing the BEncodedString</param>
        protected override void DecodeInternal(RawReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            int    letterCount;
            string length = string.Empty;

            while ((reader.PeekByte() != -1) && (reader.PeekByte() != ':')) // read in how many characters
            {
                length += (char)reader.ReadByte();                          // the string is
            }
            if (reader.ReadByte() != ':')                                   // remove the ':'
            {
                throw new BEncodingException("Invalid data found. Aborting");
            }

            if (!int.TryParse(length, out letterCount))
            {
                throw new BEncodingException(
                          string.Format("Invalid BEncodedString. Length was '{0}' instead of a number", length));
            }

            TextBytes = new byte[letterCount];
            if (reader.Read(TextBytes, 0, letterCount) != letterCount)
            {
                throw new BEncodingException("Couldn't decode string");
            }
        }