Statistics
| Branch: | Revision:

root / trunk / Pithos.Network / ByteArrayContentWithProgress.cs @ 2fdc1973

History | View | Annotate | Download (4 kB)

1 b069042e pkanavos
using System;
2 b069042e pkanavos
using System.Collections.Generic;
3 ec8c61b6 pkanavos
using System.Diagnostics;
4 b069042e pkanavos
using System.IO;
5 b069042e pkanavos
using System.Linq;
6 b069042e pkanavos
using System.Net;
7 b069042e pkanavos
using System.Net.Http;
8 b069042e pkanavos
using System.Text;
9 b069042e pkanavos
using System.Threading.Tasks;
10 b069042e pkanavos
11 b069042e pkanavos
namespace Pithos.Network
12 b069042e pkanavos
{
13 b069042e pkanavos
    public class ByteArrayContentWithProgress : ByteArrayContent
14 b069042e pkanavos
    {
15 b069042e pkanavos
        private IProgress<UploadArgs> _progress;
16 b069042e pkanavos
        private readonly byte[] _content;
17 b069042e pkanavos
        private readonly int _offset;
18 b069042e pkanavos
        private readonly int _count;
19 b069042e pkanavos
20 b069042e pkanavos
21 b069042e pkanavos
        public ByteArrayContentWithProgress(byte[] content,IProgress<UploadArgs> progress )
22 b069042e pkanavos
            : this(content,0,content.Length,progress)
23 b069042e pkanavos
        {
24 b069042e pkanavos
        }
25 b069042e pkanavos
26 b069042e pkanavos
        /// <summary>
27 b069042e pkanavos
        /// Initializes a new instance of the <see cref="T:System.Net.Http.ByteArrayContent"/> class.
28 b069042e pkanavos
        /// </summary>
29 b069042e pkanavos
        /// <param name="content">The content used to initialize the <see cref="T:System.Net.Http.ByteArrayContent"/>.</param><param name="offset">The offset, in bytes, in the <paramref name="content"/>  parameter used to initialize the <see cref="T:System.Net.Http.ByteArrayContent"/>.</param><param name="count">The number of bytes in the <paramref name="content"/> starting from the <paramref name="offset"/> parameter used to initialize the <see cref="T:System.Net.Http.ByteArrayContent"/>.</param><exception cref="T:System.ArgumentNullException">The <paramref name="content"/> parameter is null. </exception><exception cref="T:System.ArgumentOutOfRangeException">The <paramref name="offset"/> parameter is less than zero.-or-The <paramref name="offset"/> parameter is greater than the length of content specified by the <paramref name="content"/> parameter.-or-The <paramref name="count "/> parameter is less than zero.-or-The <paramref name="count"/> parameter is greater than the length of content specified by the <paramref name="content"/> parameter - minus the <paramref name="offset"/> parameter.</exception>
30 b069042e pkanavos
        public ByteArrayContentWithProgress(byte[] content, int offset, int count, IProgress<UploadArgs> progress)
31 b069042e pkanavos
            : base(content, offset, count)
32 b069042e pkanavos
        {
33 b069042e pkanavos
            if (progress== null)
34 b069042e pkanavos
                throw new ArgumentNullException("progress");
35 b069042e pkanavos
            _progress = progress;
36 b069042e pkanavos
            _content = content;
37 b069042e pkanavos
            _offset = offset;
38 b069042e pkanavos
            _count = count;
39 b069042e pkanavos
        }
40 b069042e pkanavos
41 8d38a269 pkanavos
        public ByteArrayContentWithProgress Clone()
42 8d38a269 pkanavos
        {
43 8d38a269 pkanavos
            return new ByteArrayContentWithProgress(_content,_offset,_count,_progress);
44 8d38a269 pkanavos
        }
45 8d38a269 pkanavos
46 b069042e pkanavos
        /// <summary>
47 b069042e pkanavos
        /// Serialize and write the byte array provided in the constructor to an HTTP content stream as an asynchronous operation.
48 b069042e pkanavos
        /// </summary>
49 b069042e pkanavos
        /// 
50 b069042e pkanavos
        /// <returns>
51 b069042e pkanavos
        /// Returns <see cref="T:System.Threading.Tasks.Task"/>. The task object representing the asynchronous operation.
52 b069042e pkanavos
        /// </returns>
53 b069042e pkanavos
        /// <param name="stream">The target stream.</param><param name="context">Information about the transport, like channel binding token. This parameter may be null.</param>
54 b069042e pkanavos
        protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
55 b069042e pkanavos
        {
56 f037bf1f pkanavos
            const int block_size = 65536;
57 f037bf1f pkanavos
58 ec8c61b6 pkanavos
            var watch = new Stopwatch();
59 ec8c61b6 pkanavos
60 ec8c61b6 pkanavos
61 f037bf1f pkanavos
            var end = _offset + _count;
62 f037bf1f pkanavos
            for (var idx = _offset; idx < end; idx += block_size)
63 b069042e pkanavos
            {
64 f037bf1f pkanavos
                var size = (idx > end - block_size)
65 f037bf1f pkanavos
                    ? end - idx
66 f037bf1f pkanavos
                    : block_size;
67 f037bf1f pkanavos
                var total = idx + size-_offset;
68 ec8c61b6 pkanavos
                watch.Start();
69 f037bf1f pkanavos
                await stream.WriteAsync(_content, idx, size).ConfigureAwait(false);
70 ec8c61b6 pkanavos
                watch.Stop();
71 ec8c61b6 pkanavos
72 ec8c61b6 pkanavos
                var speed = 1000 * size / watch.ElapsedMilliseconds;
73 f037bf1f pkanavos
                var percentage = Convert.ToInt32(100 * total / (double)_count);
74 ec8c61b6 pkanavos
                _progress.Report(new UploadArgs(percentage, null, total, _count, 0, 0,speed));
75 ec8c61b6 pkanavos
                
76 ec8c61b6 pkanavos
                watch.Restart();
77 b069042e pkanavos
            }
78 b069042e pkanavos
            //await Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, _content, _offset, _count, null);
79 b069042e pkanavos
        }
80 b069042e pkanavos
    }
81 b069042e pkanavos
}