Exemplo n.º 1
0
 /// <summary>
 /// Loads a torrent from the provided <see cref="BEncodedDictionary"/>
 /// </summary>
 /// <param name="dictionary">The BEncodedDictionary containing the torrent data</param>
 /// <returns></returns>
 public static Torrent Load(BEncodedDictionary dictionary)
 {
     using (MemoryPool.Default.Rent(dictionary.LengthInBytes(), out Memory <byte> buffer)) {
         dictionary.Encode(buffer.Span);
         return(Load(buffer.Span));
     }
 }
Exemplo n.º 2
0
        public void benDictionaryLengthInBytes()
        {
            byte[]             data = System.Text.Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");
            BEncodedDictionary dict = (BEncodedDictionary)BEncodedValue.Decode(data);

            Assert.AreEqual(data.Length, dict.LengthInBytes());
        }
Exemplo n.º 3
0
 public void benDictionaryEncodingBuffered()
 {
     var data = Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");
     var dict = new BEncodedDictionary();
     var list = new BEncodedList();
     list.Add(new BEncodedString("a"));
     list.Add(new BEncodedString("b"));
     dict.Add("spam", list);
     var result = new byte[dict.LengthInBytes()];
     dict.Encode(result, 0);
     Assert.True(Toolbox.ByteMatch(data, result));
 }
Exemplo n.º 4
0
        public override int Encode(Span <byte> buffer)
        {
            int written             = buffer.Length;
            BEncodedDictionary dict = Create();

            Write(ref buffer, dict.LengthInBytes() + 1 + 1);
            Write(ref buffer, MessageId);
            Write(ref buffer, Support.MessageId);
            Write(ref buffer, dict);

            return(written - buffer.Length);
        }
        public override int Encode(byte[] buffer, int offset)
        {
            int written             = offset;
            BEncodedDictionary dict = Create();

            written += Write(buffer, written, dict.LengthInBytes() + 1 + 1);
            written += Write(buffer, written, MessageId);
            written += Write(buffer, written, Support.MessageId);
            written += dict.Encode(buffer, written);

            return(written - offset);
        }
Exemplo n.º 6
0
        public void benDictionaryEncodingBuffered()
        {
            byte[]             data = System.Text.Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");
            BEncodedDictionary dict = new BEncodedDictionary();
            BEncodedList       list = new BEncodedList();

            list.Add(new BEncodedString("a"));
            list.Add(new BEncodedString("b"));
            dict.Add("spam", list);
            byte[] result = new byte[dict.LengthInBytes()];
            dict.Encode(result, 0);
            Assert.IsTrue(Toolbox.ByteMatch(data, result));
        }
Exemplo n.º 7
0
        public void BenDictionaryEncodingBuffered()
        {
            var data = Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");
            var dict = new BEncodedDictionary();
            var list = new BEncodedList
            {
                new BEncodedString("a"),
                new BEncodedString("b")
            };

            dict.Add("spam", list);
            var result = new byte[dict.LengthInBytes()];

            dict.Encode(result, 0);

            Assert.IsTrue(Toolbox.ByteMatch(data, result));
        }