Ejemplo n.º 1
0
 public void PeekAfterSeek()
 {
     var str = "abcdefghijkl";
     using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
     using (var bs = new BencodeStream(ms))
     {
         Assert.AreEqual(0, bs.Position);
         Assert.AreEqual('a', bs.PeekChar());
         bs.Seek(1, SeekOrigin.Current);
         Assert.AreEqual(1, bs.Position);
         Assert.AreEqual('b', bs.PeekChar());
     }
 }
Ejemplo n.º 2
0
 public void PeekAfterPositionChangeOnBaseStream()
 {
     var str = "abcdefghijkl";
     using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
     using (var bs = new BencodeStream(ms))
     {
         Assert.AreEqual(0, bs.Position);
         Assert.AreEqual('a', bs.PeekChar());
         bs.BaseStream.Position = 1;
         Assert.AreEqual(1, bs.Position);
         Assert.AreEqual('b', bs.PeekChar());
     }
 }
Ejemplo n.º 3
0
        public static BList DecodeList(BencodeStream stream, Encoding encoding)
        {
            if (stream == null) throw new ArgumentNullException("stream");
            if (encoding == null) throw new ArgumentNullException("encoding");

            if (stream.Length < 2)
                throw new BencodeDecodingException<BList>("Minimum valid length is 2 (an empty list: 'le')", stream.Position);

            // Lists must start with 'l'
            if (stream.ReadChar() != 'l')
                throw new BencodeDecodingException<BList>(string.Format("Must begin with 'l' but began with '{0}'.", stream.ReadPreviousChar()), 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 = Decode(stream, encoding);
                if (bObject == null)
                    throw new BencodeDecodingException<BList>(string.Format("Invalid object beginning with '{0}'", stream.PeekChar()), stream.Position);

                list.Add(bObject);
            }

            if (stream.ReadChar() != 'e')
                throw new BencodeDecodingException<BList>("Missing end character 'e'.", stream.Position);

            return list;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Decodes the specified stream using the specified encoding.
        /// </summary>
        /// <param name="stream">The stream to decode.</param>
        /// /// <param name="encoding">The encoding used by <see cref="BString"/> when calling <c>ToString()</c> with no arguments.</param>
        /// <returns>An <see cref="IBObject"/> representing the bencoded stream.</returns>
        /// <exception cref="ArgumentNullException">stream</exception>
        public static IBObject Decode(BencodeStream stream, Encoding encoding)
        {
            if (stream == null) throw new ArgumentNullException("stream");
            if (encoding == null) throw new ArgumentNullException("encoding");

            switch (stream.PeekChar())
            {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9': return DecodeString(stream, encoding);
                case 'i': return DecodeNumber(stream);
                case 'l': return DecodeList(stream, encoding);
                case 'd': return DecodeDictionary(stream, encoding);
            }

            // TODO: Throw BencodeDecodingException because next char was not a valid start of a BObject?
            return null;
        }