public override List<CalculatedFileSegment> GetCalculatedSegments(
     int segmentCount, 
     RemoteFileInfo remoteFileInfo, 
     long calculatedSegmentSize, 
     long residueBytes)
 {
     return base.GetCalculatedSegments(segmentCount, remoteFileInfo, calculatedSegmentSize, residueBytes);
 }
        /// <summary>
        /// calculate size of segment
        /// </summary>
        /// <param name="segmentCount"></param>
        /// <param name="remoteFileInfo"></param>
        /// <returns></returns>
        public virtual long CalculateSegmentSize(int segmentCount, RemoteFileInfo remoteFileInfo)
        {
            if (remoteFileInfo != null)
            {
                long calculatedSegmentSize = remoteFileInfo.FileSize / (long)segmentCount;
                return calculatedSegmentSize;
            }

            return 0;
        }
 public void TestMinimumSegmentSizeCalculation()
 {
     FileSegmentCalculator calculator = new FileSegmentSizeCalculatorWithResidueHelper();
     RemoteFileInfo remoteFileInfo = new RemoteFileInfo();
     remoteFileInfo.AcceptRanges = true;
     remoteFileInfo.FileSize = 1773484;
     int segmentCount = 6;
     List<CalculatedFileSegment> segments = calculator.GetSegments(segmentCount, remoteFileInfo);
     Assert.AreEqual(segments[0].SegmentEndPosition, 295580);
 }
 public override long CalculateResidueBytes(int segmentCount, RemoteFileInfo remoteFileInfo, long calculatedSegmentSize)
 {
     if (remoteFileInfo != null)
     {
         return remoteFileInfo.FileSize - (calculatedSegmentSize * segmentCount);
     }
     else
     {
         return 0;
     }
 }
        /// <summary>
        /// Method for calculating minimum size for file segment to download
        /// </summary>
        /// <param name="segmentCount">count of segments</param>
        /// <param name="remoteFileInfo">remote file information</param>
        /// <returns>List of calulated file segments</returns>
        public override List<CalculatedFileSegment> GetSegments(int segmentCount, RemoteFileInfo remoteFileInfo)
        {
            List<CalculatedFileSegment> calculatedSegments = new List<CalculatedFileSegment>();
            if (remoteFileInfo != null)
            {
                long calculatedSegmentSize = CalculateSegmentSize(segmentCount, remoteFileInfo);
                long residueBytes = CalculateResidueBytes(segmentCount, remoteFileInfo, calculatedSegmentSize);
                calculatedSegments = GetCalculatedSegments(segmentCount, remoteFileInfo, calculatedSegmentSize, residueBytes);
            }

            return calculatedSegments;
        }
        /// <summary>
        /// Get calculated segments of download
        /// </summary>
        /// <param name="segmentCount">count of segments</param>
        /// <param name="remoteFileInfo">remote file info</param>
        /// <param name="calculatedSegmentSize">size of calculated segment</param>
        /// <param name="residueBytes">residue bytes of download</param>
        /// <returns></returns>
        public virtual List<CalculatedFileSegment> GetCalculatedSegments(
            int segmentCount, 
            RemoteFileInfo remoteFileInfo, 
            long calculatedSegmentSize, 
            long residueBytes)
        {
            List<CalculatedFileSegment> calculatedSegments = new List<CalculatedFileSegment>();
            if (remoteFileInfo != null)
            {
                long startSegmentPosition = 0;
                for (int i = 0; i < segmentCount; i++)
                {
                    if (i != segmentCount - 1)
                    {
                        if (segmentCount == 1)
                        {
                            calculatedSegments.Add(new CalculatedFileSegment(startSegmentPosition, remoteFileInfo.FileSize));
                        }
                        else
                        {
                            calculatedSegments.Add(new CalculatedFileSegment(
                                startSegmentPosition,
                                startSegmentPosition + calculatedSegmentSize));
                        }
                    }

                    if (i == segmentCount - 1)
                    {
                        calculatedSegments.Add(new CalculatedFileSegment(
                            startSegmentPosition,
                            startSegmentPosition + calculatedSegmentSize + residueBytes));
                    }

                    startSegmentPosition = calculatedSegments[calculatedSegments.Count - 1].SegmentEndPosition;
                }
            }

            return calculatedSegments;
        }
        /// <summary>
        /// Method for getting file information
        /// </summary>
        /// <param name="resourceInfo">link</param>
        /// <param name="stream">download stream</param>
        /// <returns>information about downloading file</returns>
        public RemoteFileInfo GetFileInfo(ResourceInfo resourceInfo, out Stream stream)
        {
            if (resourceInfo != null)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resourceInfo.Url);
                request.Timeout = 30000;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                RemoteFileInfo result = new RemoteFileInfo();
                result.LastModified = response.LastModified;
                result.FileSize = response.ContentLength;
                result.AcceptRanges = string.Compare(response.Headers["Accept-Ranges"], "bytes", true, CultureInfo.CurrentCulture) == 0;

                stream = response.GetResponseStream();

                return result;
            }
            else
            {
                stream = null;
                return null;
            }
        }
 public virtual List<CalculatedFileSegment> GetSegments(int segmentCount, RemoteFileInfo remoteFileInfo)
 {
     long calculatedSegmentSize = CalculateSegmentSize(segmentCount, remoteFileInfo);
     long residueBytes = CalculateResidueBytes(segmentCount, remoteFileInfo, calculatedSegmentSize);
     return GetCalculatedSegments(segmentCount, remoteFileInfo, calculatedSegmentSize, residueBytes);
 }
 /// <summary>
 /// calculate residue size bytes after dividing fole on segments
 /// </summary>
 /// <param name="segmentCount">count of segments</param>
 /// <param name="remoteFileInfo">information about remote file</param>
 /// <param name="calculatedSegmentSize"></param>
 /// <returns></returns>
 public virtual long CalculateResidueBytes(int segmentCount, RemoteFileInfo remoteFileInfo, long calculatedSegmentSize)
 {
     return 0;
 }
 private static List<CalculatedFileSegment> GetSegmentsFromCalculator()
 {
     FileSegmentCalculator calculator = new FileSegmentSizeCalculatorWithResidueHelper();
     RemoteFileInfo remoteFileInfo = new RemoteFileInfo();
     remoteFileInfo.AcceptRanges = true;
     remoteFileInfo.FileSize = 4724748;
     int segmentCount = 5;
     List<CalculatedFileSegment> segments = calculator.GetSegments(segmentCount, remoteFileInfo);
     return segments;
 }
 public override long CalculateSegmentSize(int segmentCount, RemoteFileInfo remoteFileInfo)
 {
     return base.CalculateSegmentSize(segmentCount, remoteFileInfo);
 }
        public Downloader Add(
            ResourceInfo resourceInfo, 
            ResourceInfo[] mirrors, 
            string localFile, 
            List<FileSegment> segments, 
            RemoteFileInfo remoteInfo,  
            bool autoStart, 
            DateTime createdDateTime,
            string fileName)
        {
            Downloader d = new Downloader(resourceInfo, mirrors, localFile, segments, remoteInfo, createdDateTime, fileName);
            Add(d, autoStart);

            return d;
        }