void CheckFileOrCreateFile()
 {
     DownloaderHelper.CheckFileOrCreateFile(this, fileLocker);
 }
        void DownloadInternal()
        {
            if (Status != DownloadStatus.Waiting)
            {
                return;
            }

            HttpWebRequest  webRequest     = null;
            HttpWebResponse webResponse    = null;
            Stream          responseStream = null;
            MemoryStream    downloadCache  = null;

            lastStartTime = DateTime.Now;

            try
            {
                if (!HasChecked)
                {
                    string filename = string.Empty;
                    CheckUrlAndFile(out filename);
                }

                EnsurePropertyValid();

                Status = DownloadStatus.Downloading;

                webRequest = DownloaderHelper.InitializeHttpWebRequest(this);

                if (EndPoint != int.MaxValue)
                {
                    webRequest.AddRange(StartPoint + DownloadedSize, EndPoint);
                }
                else
                {
                    webRequest.AddRange(StartPoint + DownloadedSize);
                }

                webResponse = (HttpWebResponse)webRequest.GetResponse();

                responseStream = webResponse.GetResponseStream();

                downloadCache = new MemoryStream(MaxCacheSize);

                byte[] downloadBuffer = new byte[BufferSize];

                int bytesSize = 0;
                CachedSize = 0;
                int receivedBufferCount = 0;

                while (true)
                {
                    bytesSize = responseStream.Read(downloadBuffer, 0, downloadBuffer.Length);

                    if (Status != DownloadStatus.Downloading || bytesSize == 0 || MaxCacheSize < CachedSize + bytesSize)
                    {
                        try
                        {
                            WriteCacheToFile(downloadCache, CachedSize);

                            DownloadedSize += CachedSize;

                            if (Status != DownloadStatus.Downloading || bytesSize == 0)
                            {
                                break;
                            }

                            downloadCache.Seek(0, SeekOrigin.Begin);
                            CachedSize = 0;
                        }
                        catch (Exception ex)
                        {
                            OnDownloadCompleted(new DownloadCompletedEventArgs(null, DownloadedSize, TotalSize, TotalUsedTime, ex));
                            return;
                        }
                    }

                    downloadCache.Write(downloadBuffer, 0, bytesSize);

                    CachedSize += bytesSize;

                    receivedBufferCount++;

                    if (receivedBufferCount == BufferCountPerNotification)
                    {
                        InternalDownloadProgressChanged(CachedSize);
                        receivedBufferCount = 0;
                    }
                }

                usedTime = usedTime.Add(DateTime.Now - lastStartTime);

                if (Status == DownloadStatus.Pausing)
                {
                    Status = DownloadStatus.Paused;
                }
                else if (Status == DownloadStatus.Canceling)
                {
                    Status = DownloadStatus.Canceled;
                }
                else
                {
                    Status = DownloadStatus.Completed;
                    return;
                }
            }
            catch (Exception ex)
            {
                OnDownloadCompleted(new DownloadCompletedEventArgs(null, DownloadedSize, TotalSize, TotalUsedTime, ex));
                return;
            }
            finally
            {
                if (responseStream != null)
                {
                    responseStream.Close();
                }
                if (webResponse != null)
                {
                    webResponse.Close();
                }
                if (downloadCache != null)
                {
                    downloadCache.Close();
                }
            }
        }
 public void CheckUrl(out string fileName)
 {
     fileName = DownloaderHelper.CheckUrl(this);
 }