Example #1
0
        public BTItem ReadDictionary(FileStream sr)
        {
            BTDictionary d = new BTDictionary();
            for (;;)
            {
                BTItem next = this.ReadNext(sr);
                if ((next.Type == BTChunk.kListOrDictionaryEnd) || (next.Type == BTChunk.kBTEOF))
                    return d;

                if (next.Type != BTChunk.kString)
                {
                    BTError e = new BTError();
                    e.Message = "Didn't get string as first of pair in dictionary";
                    return e;
                }

                BTDictionaryItem di = new BTDictionaryItem
                                          {
                                              Key = ((BTString)next).AsString(),
                                              Data = this.ReadNext(sr)
                                          };

                d.Items.Add(di);
            }
        }
Example #2
0
        public BTItem ReadDictionary(System.IO.FileStream sr)
        {
            BTDictionary d = new BTDictionary();

            for (;;)
            {
                BTItem next = ReadNext(sr);
                if ((next.Type == BTChunk.kListOrDictionaryEnd) || (next.Type == BTChunk.kBTEOF))
                {
                    return(d);
                }

                if (next.Type != BTChunk.kString)
                {
                    BTError e = new BTError();
                    e.Message = "Didn't get string as first of pair in dictionary";
                    return(e);
                }

                BTDictionaryItem di = new BTDictionaryItem
                {
                    Key  = ((BTString)next).AsString(),
                    Data = ReadNext(sr)
                };

                d.Items.Add(di);
            }
        }
Example #3
0
        public BTItem ReadNext(System.IO.FileStream sr)
        {
            if (sr.Length == sr.Position)
            {
                return(new BTEOF());
            }

            // Read the next character from the stream to see what is next

            int c = sr.ReadByte();

            if (c == 'd')
            {
                return(ReadDictionary(sr)); // dictionary
            }
            if (c == 'l')
            {
                return(ReadList(sr)); // list
            }
            if (c == 'i')
            {
                return(ReadInt(sr)); // integer
            }
            if (c == 'e')
            {
                return(new BTListOrDictionaryEnd()); // end of list/dictionary/etc.
            }
            if ((c >= '0') && (c <= '9'))            // digits mean it is a string of the specified length
            {
                string r = Convert.ToString(c - '0');
                while ((c = sr.ReadByte()) != ':')
                {
                    r += Convert.ToString(c - '0');
                }

                return(ReadString(sr, Convert.ToInt32(r)));
            }

            BTError e = new BTError
            {
                Message = $"Error: unknown BEncode item type: {c}"
            };

            return(e);
        }
Example #4
0
        public BTItem ReadNext(FileStream sr)
        {
            if (sr.Length == sr.Position)
                return new BTEOF();

            // Read the next character from the stream to see what is next

            int c = sr.ReadByte();
            if (c == 'd')
                return this.ReadDictionary(sr); // dictionary
            if (c == 'l')
                return this.ReadList(sr); // list
            if (c == 'i')
                return this.ReadInt(sr); // integer
            if (c == 'e')
                return new BTListOrDictionaryEnd(); // end of list/dictionary/etc.
            if ((c >= '0') && (c <= '9')) // digits mean it is a string of the specified length
            {
                string r = Convert.ToString(c - '0');
                while ((c = sr.ReadByte()) != ':')
                    r += Convert.ToString(c - '0');
                return this.ReadString(sr, Convert.ToInt32(r));
            }

            BTError e = new BTError {
                                        Message = String.Concat("Error: unknown BEncode item type: ", c)
                                    };
            return e;
        }