Author: alanmc
Date: 2007-08-21 20:31:00 -0400 (Tue, 21 Aug 2007)
New Revision: 84601

Modified:
   trunk/bitsharp/src/MonoTorrent/Common/TorrentCreator.cs
Log:
Bugfix: Synchronous creation now works. Features: Can now write the resulting 
.torrent to a stream, file, or return the raw Dictionary in both the 
synchronous and asynchronous case.

Modified: trunk/bitsharp/src/MonoTorrent/Common/TorrentCreator.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Common/TorrentCreator.cs     2007-08-22 
00:12:58 UTC (rev 84600)
+++ trunk/bitsharp/src/MonoTorrent/Common/TorrentCreator.cs     2007-08-22 
00:31:00 UTC (rev 84601)
@@ -211,31 +211,49 @@
             return result;
         }
 
+        /// <summary>
+        /// Creates a Torrent and returns it in it's dictionary form
+        /// </summary>
+        /// <returns></returns>
         public BEncodedDictionary Create()
         {
-            Reset();
-            CreateDict();
-
-            // Return a clone of the internal dictionary
-            return 
BEncodedValue.Decode<BEncodedDictionary>(this.torrent.Encode());
+            // Use the async methods behind the scenes to do this
+            return EndCreate(BeginCreate(null, null));
         }
 
         ///<summary>
-        ///creates and stores a torrent at storagePath
+        /// Creates a Torrent and writes it to disk in the specified location
         ///<summary>
-        ///<param name="storagePath">place and name to store the torrent 
file</param>
+        ///<param name="storagePath">The path (including filename) where the 
new Torrent will be written to</param>
         public void Create(string path)
         {
-            Reset();
-            CreateDict();
+            if (string.IsNullOrEmpty(path))
+                throw new ArgumentNullException("path");
 
             using (FileStream stream = new FileStream(path, FileMode.Create))
-            {
-                byte[] data = this.torrent.Encode();
-                stream.Write(data, 0, data.Length);
-            }
+                Create(stream);
         }
 
+        /// <summary>
+        /// Generates a Torrent and writes it to the supplied stream
+        /// </summary>
+        /// <param name="stream">The stream to write the torrent to</param>
+        public void Create(Stream stream)
+        {
+            if (stream == null)
+                throw new ArgumentNullException("stream");
+
+            Create();
+
+            byte[] data = this.torrent.Encode();
+            stream.Write(data, 0, data.Length);
+        }
+
+        /// <summary>
+        /// Ends the asynchronous torrent creation and returns the torrent in 
it's dictionary form
+        /// </summary>
+        /// <param name="result"></param>
+        /// <returns></returns>
         public BEncodedDictionary EndCreate(IAsyncResult result)
         {
             if (result == null)
@@ -252,7 +270,7 @@
                 if (this.result.SavedException != null)
                     throw this.result.SavedException;
 
-                return this.result.Aborted ? null : this.torrent;
+                return this.result.Aborted ? null : 
BEncodedValue.Decode<BEncodedDictionary>(this.torrent.Encode());
             }
             finally
             {
@@ -260,6 +278,34 @@
             }
         }
 
+        /// <summary>
+        /// Ends the asynchronous torrent creation and writes the torrent to 
the specified path
+        /// </summary>
+        /// <param name="result"></param>
+        /// <returns></returns>
+        public void EndCreate(IAsyncResult result, string path)
+        {
+            if (string.IsNullOrEmpty(path))
+                throw new ArgumentNullException("path");
+
+            using (FileStream s = File.OpenWrite(path))
+                EndCreate(result, s);
+        }
+
+        /// <summary>
+        /// Ends the asynchronous torrent creation and writes the torrent to 
the specified stream
+        /// </summary>
+        /// <param name="result"></param>
+        /// <returns></returns>
+        public void EndCreate(IAsyncResult result, Stream stream)
+        {
+            if (stream == null)
+                throw new ArgumentNullException("stream");
+
+            byte[] data = EndCreate(result).Encode();
+            stream.Write(data, 0, data.Length);
+        }
+
         ///<summary>
         /// Calculates the approximate size of the final .torrent in bytes
         ///</summary>
@@ -311,7 +357,8 @@
         {
             try
             {
-                Create();
+                Reset();
+                CreateDict();
             }
             catch (Exception ex)
             {

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

Reply via email to