Now adding a file's MIME type when uploading. The MIME type is retrieved from the...
[pithos-ms-client] / trunk / Pithos.Network / ByteArrayContentWithProgress.cs
index 01c85f8..9dbe6df 100644 (file)
@@ -1,5 +1,6 @@
 using System;\r
 using System.Collections.Generic;\r
+using System.Diagnostics;\r
 using System.IO;\r
 using System.Linq;\r
 using System.Net;\r
@@ -37,6 +38,11 @@ namespace Pithos.Network
             _count = count;\r
         }\r
 \r
+        public ByteArrayContentWithProgress Clone()\r
+        {\r
+            return new ByteArrayContentWithProgress(_content,_offset,_count,_progress);\r
+        }\r
+\r
         /// <summary>\r
         /// Serialize and write the byte array provided in the constructor to an HTTP content stream as an asynchronous operation.\r
         /// </summary>\r
@@ -47,21 +53,29 @@ namespace Pithos.Network
         /// <param name="stream">The target stream.</param><param name="context">Information about the transport, like channel binding token. This parameter may be null.</param>\r
         protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)\r
         {\r
-            using(var mem=new MemoryStream(_content,_offset,_count,false))\r
+            const int block_size = 65536;\r
+\r
+            var watch = new Stopwatch();\r
+\r
+\r
+            var end = _offset + _count;\r
+            for (var idx = _offset; idx < end; idx += block_size)\r
             {\r
-                long total = 0;\r
-                var buffer = new byte[65536];\r
-                int read;\r
-                while ((read = mem.Read(buffer, 0, buffer.Length)) != 0)\r
-                {\r
-                    total += read;\r
-                    var percentage = Convert.ToInt32(100*total/(double) _count);\r
-                    _progress.Report(new UploadArgs(percentage,null,total,_count,0,0));\r
-                    await stream.WriteAsync(buffer, 0, read).ConfigureAwait(false);\r
-                }\r
+                var size = (idx > end - block_size)\r
+                    ? end - idx\r
+                    : block_size;\r
+                var total = idx + size-_offset;\r
+                watch.Start();\r
+                await stream.WriteAsync(_content, idx, size).ConfigureAwait(false);\r
+                watch.Stop();\r
+\r
+                var speed = 1000 * size / watch.ElapsedMilliseconds;\r
+                var percentage = Convert.ToInt32(100 * total / (double)_count);\r
+                _progress.Report(new UploadArgs(percentage, null, total, _count, 0, 0,speed));\r
+                \r
+                watch.Restart();\r
             }\r
             //await Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, _content, _offset, _count, null);\r
         }\r
-\r
     }\r
 }\r