public void Download(byte[] data, int bufferSize, DownloadSegmentPositions[] segmentPositionInfos)
        {
            BeforeDownload();

            DownloadStream inputStream = new DownloadStream(data);

            IList<ISegmentDownloadTask> segmentDownloadTasks = new List<ISegmentDownloadTask>(segmentPositionInfos.Length);
            IList<DownloadStream> downloadStreams = new List<DownloadStream>(segmentPositionInfos.Length);
            MemoryStream outputStream = new MemoryStream();
            for (int i = 0; i < segmentPositionInfos.Length; i++)
            {
                DownloadSegmentPositions segmentPosition = segmentPositionInfos[i];
                byte[] dataPart = data.Skip((int) segmentPosition.StartPosition).Take((int)(segmentPosition.EndPosition - segmentPosition.StartPosition + 1)).ToArray();
                DownloadStream downloadStream = new DownloadStream(dataPart);
                segmentDownloadTasks.Add(CreateSegmentDownloadTask(bufferSize, CreateSegmentDownloader(downloadStream, segmentPosition), CreateSegmentWriter(outputStream)));
                downloadStreams.Add(downloadStream);
            }

            SegmentDownloadManager segmentDownloadManager = new SegmentDownloadManager(new SegmentDownloadTaskCollection(segmentDownloadTasks));
            segmentDownloadManager.Start();
            segmentDownloadManager.Finish(true);

            AfterDownload();

            long totalDownloads = downloadStreams.Sum(x => x.TotalDownloads);
            Assert.AreEqual(data.Length, totalDownloads);
            Assert.AreEqual(inputStream.ToArray().Take(data.Length).ToArray(), outputStream.ToArray().Take(data.Length).ToArray());
            Assert.AreEqual(inputStream.ToArray(), outputStream.ToArray());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SegmentDownloader"/> class.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="downloadSegmentInfo">The download segment information.</param>
        /// <param name="segmentDownloadRateCalculator">The segment download rate calculator.</param>
        /// <exception cref="System.ArgumentNullException">downloadSegmentInfo</exception>
        public SegmentDownloader(Stream stream, DownloadSegmentPositions downloadSegmentInfo, ISegmentDownloadRateCalculator segmentDownloadRateCalculator)
        {
            if (downloadSegmentInfo == null)
            {
                throw new ArgumentNullException("downloadSegmentInfo");
            }

            m_Stream = stream;
            m_SegmentDownloadRateCalculator = segmentDownloadRateCalculator;
            m_StartPosition = downloadSegmentInfo.StartPosition;
            m_EndPosition = downloadSegmentInfo.EndPosition;
            m_CurrentPosition = StartPosition;
        }
        public void Calculate(int minimumSegmentSize, int maximumSegmentCount, int segmentCount, int fileSize, DownloadSegmentPositions[] expectedPositions)
        {
            DownloadSegmentPositionsCalculator downloadSegmentPositionsCalculator = new DownloadSegmentPositionsCalculator();
            DownloadSegmentPositions[] downloadSegmentPositions = downloadSegmentPositionsCalculator.Calculate(minimumSegmentSize, maximumSegmentCount, segmentCount, fileSize);

            Assert.AreEqual(expectedPositions.Length, downloadSegmentPositions.Length);

            for (int i = 0; i < expectedPositions.Length; i++)
            {
                Assert.AreEqual(expectedPositions[i].StartPosition, downloadSegmentPositions[i].StartPosition);
                Assert.AreEqual(expectedPositions[i].EndPosition,   downloadSegmentPositions[i].EndPosition);
            }
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SegmentDownloader"/> class.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="downloadSegmentInfo">The download segment information.</param>
        /// <param name="segmentDownloadRateCalculator">The segment download rate calculator.</param>
        /// <exception cref="System.ArgumentNullException">downloadSegmentInfo</exception>
        public SegmentDownloader(Stream stream, DownloadSegmentPositions downloadSegmentInfo, ISegmentDownloadRateCalculator segmentDownloadRateCalculator)
        {
            if (downloadSegmentInfo == null)
            {
                throw new ArgumentNullException("downloadSegmentInfo");
            }

            m_Stream = stream;
            m_SegmentDownloadRateCalculator = segmentDownloadRateCalculator;
            m_StartPosition   = downloadSegmentInfo.StartPosition;
            m_EndPosition     = downloadSegmentInfo.EndPosition;
            m_CurrentPosition = StartPosition;
        }
        public void Download(byte[] data, int bufferSize, int endPosition)
        {
            BeforeDownload();

            DownloadStream inputStream = new DownloadStream(data);

            MemoryStream outputStream = new MemoryStream();
            DownloadSegmentPositions downloadSegmentPositions = new DownloadSegmentPositions(0, endPosition);

            ISegmentDownloadTask task = CreateSegmentDownloadTask(bufferSize, CreateSegmentDownloader(inputStream, downloadSegmentPositions), CreateSegmentWriter(outputStream));
            task.Download();

            AfterDownload();

            Assert.AreEqual(endPosition + 1, inputStream.TotalDownloads);
            Assert.AreEqual(inputStream.ToArray().Take(endPosition).ToArray(), outputStream.ToArray().Take(endPosition).ToArray());
        }
        public DownloadSegmentPositions[] Calculate(int minimumSegmentSize, int maximumSegmentCount, int segmentCount, long fileSize)
        {
            if (fileSize <= 0)
            {
                return(new[] { new DownloadSegmentPositions(-1, -1) });
            }

            if (fileSize <= minimumSegmentSize)
            {
                return(new[] { new DownloadSegmentPositions(0, fileSize - 1) });
            }

            segmentCount = Math.Min(maximumSegmentCount, segmentCount == 0 ? 1 : segmentCount);

            long segmentSize = fileSize / segmentCount;

            while (segmentCount > 1 && segmentSize < minimumSegmentSize)
            {
                segmentCount--;
                segmentSize = fileSize / segmentCount;
            }

            DownloadSegmentPositions[] segmentPositions = new DownloadSegmentPositions[segmentCount];
            for (int i = 0; i < segmentCount; i++)
            {
                long startPosition = i * segmentSize;
                long endPosition;
                if (i == segmentCount - 1)
                {
                    endPosition = fileSize - 1;
                }
                else
                {
                    endPosition = startPosition + segmentSize - 1;
                }
                segmentPositions[i] = new DownloadSegmentPositions(startPosition, endPosition);
            }
            return(segmentPositions);
        }
        public DownloadSegmentPositions[] Calculate(int minimumSegmentSize, int maximumSegmentCount, int segmentCount, long fileSize)
        {
            if (fileSize <= 0)
            {
                return new[] { new DownloadSegmentPositions(-1, -1) };
            }

            if (fileSize <= minimumSegmentSize)
            {
                return new[] { new DownloadSegmentPositions(0, fileSize - 1) };
            }

            segmentCount = Math.Min(maximumSegmentCount, segmentCount == 0 ? 1 : segmentCount);

            long segmentSize = fileSize/segmentCount;
            while (segmentCount > 1 && segmentSize < minimumSegmentSize)
            {
                segmentCount--; 
                segmentSize = fileSize/segmentCount;
            }

            DownloadSegmentPositions[] segmentPositions = new DownloadSegmentPositions[segmentCount];
            for (int i = 0; i < segmentCount; i++)
            {
                long startPosition = i*segmentSize;
                long endPosition;
                if(i == segmentCount - 1)
                {
                    endPosition = fileSize - 1;
                }
                else
                {
                    endPosition = startPosition + segmentSize - 1;                    
                }
                segmentPositions[i] = new DownloadSegmentPositions(startPosition, endPosition);
            }
            return segmentPositions;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SegmentDownloader"/> class.
 /// </summary>
 /// <param name="file">The file.</param>
 /// <param name="networkProtocolProvider">The network protocol provider.</param>
 /// <param name="downloadSegmentInfo">The download segment information.</param>
 /// <param name="segmentDownloadRateCalculator">The segment download rate calculator.</param>
 public SegmentDownloader(DownloadFileInfo file, INetworkProtocolProvider networkProtocolProvider, DownloadSegmentPositions downloadSegmentInfo, ISegmentDownloadRateCalculator segmentDownloadRateCalculator)
     : this(null, downloadSegmentInfo, segmentDownloadRateCalculator)
 {
     m_File = file;
     m_NetworkProtocolProvider = networkProtocolProvider;
 }
 protected virtual ISegmentDownloader CreateSegmentDownloader(Stream inputStream, DownloadSegmentPositions downloadSegmentPositions)
 {
     return new SegmentDownloader(inputStream, downloadSegmentPositions, new SegmentDownloadRateCalculator(downloadSegmentPositions.StartPosition));
 }
Пример #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SegmentDownloader"/> class.
 /// </summary>
 /// <param name="file">The file.</param>
 /// <param name="networkProtocolProvider">The network protocol provider.</param>
 /// <param name="downloadSegmentInfo">The download segment information.</param>
 /// <param name="segmentDownloadRateCalculator">The segment download rate calculator.</param>
 public SegmentDownloader(DownloadFileInfo file, INetworkProtocolProvider networkProtocolProvider, DownloadSegmentPositions downloadSegmentInfo, ISegmentDownloadRateCalculator segmentDownloadRateCalculator)
     : this(null, downloadSegmentInfo, segmentDownloadRateCalculator)
 {
     m_File = file;
     m_NetworkProtocolProvider = networkProtocolProvider;
 }