Author: alanmc
Date: 2007-05-23 20:16:42 -0400 (Wed, 23 May 2007)
New Revision: 77879

Modified:
   trunk/bitsharp/src/MonoTorrent/BEncoding/BEncodedList.cs
   trunk/bitsharp/src/MonoTorrent/BEncoding/BEncodedString.cs
   trunk/bitsharp/src/MonoTorrent/BEncoding/IBEncodedValue.cs
   trunk/bitsharp/src/MonoTorrent/Common/ToolBox.cs
   trunk/bitsharp/src/MonoTorrent/Tracker/SimpleTorrentManager.cs
Log:
Further bencoding nicities

Modified: trunk/bitsharp/src/MonoTorrent/BEncoding/BEncodedList.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/BEncoding/BEncodedList.cs    2007-05-23 
23:43:32 UTC (rev 77878)
+++ trunk/bitsharp/src/MonoTorrent/BEncoding/BEncodedList.cs    2007-05-24 
00:16:42 UTC (rev 77879)
@@ -41,7 +41,9 @@
     public class BEncodedList : BEncodedValue, IList<BEncodedValue>
     {
         #region Member Variables
+
         private List<BEncodedValue> list;
+
         #endregion
 
 
@@ -64,14 +66,11 @@
 
         }
 
-        /// <summary>
-        /// Creates a new BEncoded list from the supplied List
-        /// </summary>
-        /// <param name="value">The list to use to create the 
BEncodedList</param>
-        public BEncodedList(List<BEncodedValue> value)
+        private BEncodedList(List<BEncodedValue> value)
         {
             this.list = value;
         }
+
         #endregion
 
 
@@ -89,8 +88,8 @@
             int written = 0;
             buffer[offset] = (byte)'l';
             written++;
-            foreach (BEncodedValue value in this.list)
-                written += value.Encode(buffer, offset + written);
+            for (int i = 0; i < this.list.Count; i++)
+                written += this.list[i].Encode(buffer, offset + written);
             buffer[offset + written] = (byte)'e';
             written++;
             return written;
@@ -135,8 +134,8 @@
             int length = 0;
 
             length += 1;   // Lists start with 'l'
-            foreach (BEncodedValue item in this.list)
-                length += item.LengthInBytes();
+            for (int i=0; i < this.list.Count; i++)
+                length += this.list[i].LengthInBytes();
 
             length += 1;   // Lists end with 'e'
             return length;
@@ -147,13 +146,13 @@
         #region Overridden Methods
         public override bool Equals(object obj)
         {
-            BEncodedList obj2 = obj as BEncodedList;
+            BEncodedList other = obj as BEncodedList;
 
-            if (obj2 == null)
+            if (other == null)
                 return false;
 
             for (int i = 0; i < this.list.Count; i++)
-                if (this.list[i] != obj2.list[i])
+                if (!this.list[i].Equals(other.list[i]))
                     return false;
 
             return true;
@@ -162,7 +161,11 @@
 
         public override int GetHashCode()
         {
-            return this.list.GetHashCode();
+            int result = 0;
+            for (int i = 0; i < list.Count; i++)
+                result ^= list[i].GetHashCode();
+
+            return result;
         }
 
 
@@ -170,8 +173,8 @@
         {
             StringBuilder sb = new StringBuilder(32);
 
-            foreach (BEncodedValue value in this.list)
-                sb.Append(value.ToString());
+            for (int i = 0; i < list.Count; i++)
+                sb.Append(list[i].ToString());
 
             return sb.ToString();
         }

Modified: trunk/bitsharp/src/MonoTorrent/BEncoding/BEncodedString.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/BEncoding/BEncodedString.cs  2007-05-23 
23:43:32 UTC (rev 77878)
+++ trunk/bitsharp/src/MonoTorrent/BEncoding/BEncodedString.cs  2007-05-24 
00:16:42 UTC (rev 77879)
@@ -32,6 +32,7 @@
 using System.IO;
 using System.Collections.Generic;
 using System.Text;
+using MonoTorrent.Common;
 
 namespace MonoTorrent.BEncoding
 {
@@ -80,9 +81,6 @@
         {
         }
 
-
-
-
         /// <summary>
         /// Create a new BEncodedString using UTF8 encoding
         /// </summary>
@@ -147,21 +145,19 @@
                 throw new ArgumentNullException("reader");
 
             int letterCount;
-            StringBuilder sb = new StringBuilder(20);
+            string length = string.Empty;
 
             try
             {
-                while ((reader.PeekChar() != -1) && (reader.PeekChar() != 
':'))     // read in how many characters
-                    sb.Append((char)reader.ReadChar());                        
         // the string is
+                while ((reader.PeekChar() != -1) && (reader.PeekChar() != 
':'))         // read in how many characters
+                    length += (char)reader.ReadChar();                         
        // the string is
 
                 if (reader.ReadChar() != ':')                                  
         // remove the ':'
                     throw new BEncodingException("Invalid data found. 
Aborting");
 
-                letterCount = int.Parse(sb.ToString());
-                sb.Remove(0, sb.Length);
+                letterCount = int.Parse(length);
 
                 this.textBytes = new byte[letterCount];
-
                 if (reader.Read(textBytes, 0, letterCount) != letterCount)
                     throw new BEncodingException("Couldn't decode string");
             }
@@ -216,14 +212,7 @@
             if (other == null)
                 return false;
 
-            if (this.textBytes.Length != other.textBytes.Length)
-                return false;
-
-            for (int i = 0; i < this.textBytes.Length; i++)
-                if (this.textBytes[i] != other.textBytes[i])
-                    return false;
-
-            return true;
+            return ToolBox.ByteMatch(this.textBytes, other.textBytes);
         }
 
         public override int GetHashCode()

Modified: trunk/bitsharp/src/MonoTorrent/BEncoding/IBEncodedValue.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/BEncoding/IBEncodedValue.cs  2007-05-23 
23:43:32 UTC (rev 77878)
+++ trunk/bitsharp/src/MonoTorrent/BEncoding/IBEncodedValue.cs  2007-05-24 
00:16:42 UTC (rev 77879)
@@ -40,6 +40,8 @@
     /// </summary>
     public abstract class BEncodedValue
     {
+        internal abstract void DecodeInternal(BinaryReader reader);
+
         /// <summary>
         /// Encodes the BEncodedValue into a byte array
         /// </summary>
@@ -47,14 +49,12 @@
         public byte[] Encode()
         {
             byte[] buffer = new byte[LengthInBytes()];
-
             if (Encode(buffer, 0) != buffer.Length)
                 throw new BEncodingException("Error encoding the data");
 
             return buffer;
         }
 
-        internal abstract void DecodeInternal(BinaryReader reader);
 
         /// <summary>
         /// Encodes the BEncodedValue into the supplied buffer
@@ -96,8 +96,7 @@
                 throw new ArgumentOutOfRangeException("length");
 
             using (MemoryStream stream = new MemoryStream(buffer, offset, 
length))
-            using (BinaryReader reader = new BinaryReader(stream, 
Encoding.UTF8))
-                return Decode(reader);
+                return (BEncodedValue.Decode(stream));
         }
 
 
@@ -111,8 +110,7 @@
             if (stream == null)
                 throw new ArgumentNullException("stream");
 
-            using (BinaryReader reader = new BinaryReader(stream, 
Encoding.UTF8))
-                return Decode(reader);
+            return Decode(new BinaryReader(stream, Encoding.UTF8));
         }
 
 
@@ -163,6 +161,41 @@
         }
 
 
+        /// <summary>
+        /// Interface for all BEncoded values
+        /// </summary>
+        /// <param name="data">The byte array containing the BEncoded 
data</param>
+        /// <returns></returns>
+        public static T Decode<T>(byte[] data) where T : BEncodedValue
+        {
+            return (T)BEncodedValue.Decode(data);
+        }
+
+
+        /// <summary>
+        /// Decode BEncoded data in the given byte array
+        /// </summary>
+        /// <param name="buffer">The byte array containing the BEncoded 
data</param>
+        /// <param name="offset">The offset at which the data starts at</param>
+        /// <param name="length">The number of bytes to be decoded</param>
+        /// <returns>BEncodedValue containing the data that was in the 
byte[]</returns>
+        public static T Decode<T>(byte[] buffer, int offset, int length) where 
T : BEncodedValue
+        {
+            return (T)BEncodedValue.Decode(buffer, offset, length);
+        }
+
+
+        /// <summary>
+        /// Decode BEncoded data in the given stream 
+        /// </summary>
+        /// <param name="stream">The stream containing the BEncoded 
data</param>
+        /// <returns>BEncodedValue containing the data that was in the 
stream</returns>
+        public static T Decode<T>(Stream stream) where T : BEncodedValue
+        {
+            return (T)BEncodedValue.Decode(stream);
+        }
+
+
         public static T Decode<T>(BinaryReader reader) where T : BEncodedValue
         {
             return (T)BEncodedValue.Decode(reader);
@@ -175,4 +208,4 @@
         /// <returns></returns>
         public abstract int LengthInBytes();
     }
-}
\ No newline at end of file
+}

Modified: trunk/bitsharp/src/MonoTorrent/Common/ToolBox.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Common/ToolBox.cs    2007-05-23 23:43:32 UTC 
(rev 77878)
+++ trunk/bitsharp/src/MonoTorrent/Common/ToolBox.cs    2007-05-24 00:16:42 UTC 
(rev 77879)
@@ -36,27 +36,27 @@
 {
     public class ToolBox
     {
-               private static Random r = new Random();
+        private static Random r = new Random();
 
-               public static void Randomize<T>(List<T> array)
-               {
-                       List<T> clone = new List<T>(array);
-                       array.Clear();
+        public static void Randomize<T>(List<T> array)
+        {
+            List<T> clone = new List<T>(array);
+            array.Clear();
 
-                       while (clone.Count > 0)
-                       {
-                               int index = r.Next(0, clone.Count);
-                               array.Add(clone[index]);
-                               clone.RemoveAt(index);
-                       }
-               }
+            while (clone.Count > 0)
+            {
+                int index = r.Next(0, clone.Count);
+                array.Add(clone[index]);
+                clone.RemoveAt(index);
+            }
+        }
 
-               public static void Switch<T>(T[] array, int first, int second)
-               {
-                       T obj = array[first];
-                       array[first] = array[second];
-                       array[second] = obj;
-               }
+        public static void Switch<T>(T[] array, int first, int second)
+        {
+            T obj = array[first];
+            array[first] = array[second];
+            array[second] = obj;
+        }
 
         public static string GetHex(byte[] infoHash)
         {
@@ -79,8 +79,10 @@
         /// <returns>True if the arrays are equal, false if they 
aren't</returns>
         public static bool ByteMatch(byte[] array1, byte[] array2)
         {
-            if (array1.Length != array2.Length)      // If the arrays are 
different lengths, then they are not equal
-                return false;
+            if (array1 == null)
+                throw new ArgumentNullException("array1");
+            if (array2 == null)
+                throw new ArgumentNullException("array2");
 
             return ByteMatch(array1, array2, 0, 0, array1.Length);
         }
@@ -89,9 +91,12 @@
         {
             if (array1 == null)
                 throw new ArgumentNullException("array1");
-            if(array2 == null)
+            if (array2 == null)
                 throw new ArgumentNullException("array2");
 
+            if (array1.Length != array2.Length)      // If the arrays are 
different lengths, then they are not equal
+                return false;
+
             for (int i = 0; i < count; i++)
                 if (array1[offset1 + i] != array2[offset2 + i]) // For each 
element, if it is not the same in both
                     return false;                               // arrays, 
return false
@@ -99,4 +104,4 @@
             return true;                            // If we get here, all the 
elements matched, so they are equal
         }
     }
-}
\ No newline at end of file
+}

Modified: trunk/bitsharp/src/MonoTorrent/Tracker/SimpleTorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Tracker/SimpleTorrentManager.cs      
2007-05-23 23:43:32 UTC (rev 77878)
+++ trunk/bitsharp/src/MonoTorrent/Tracker/SimpleTorrentManager.cs      
2007-05-24 00:16:42 UTC (rev 77879)
@@ -175,14 +175,14 @@
         private BEncodedValue GetNonCompactList(AnnounceParameters par)
         {
             Peer exclude = peers.Get(Peer.GetKey(par));            
-            IList<Peer> randomPeers = peers.GetRandomPeers(par.numberWanted, 
exclude);            
-            List<BEncodedValue> announceList = new 
List<BEncodedValue>(randomPeers.Count);
+            IList<Peer> randomPeers = peers.GetRandomPeers(par.numberWanted, 
exclude);
+            BEncodedList announceList = new BEncodedList(randomPeers.Count);
             
             foreach (Peer each in randomPeers) {
                 announceList.Add(each.PeersEntry);
             }
             
-            return new BEncodedList(announceList);
+            return announceList;
         }
         
         public BEncodedDictionary GetScrapeEntry()

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to