Ejemplo n.º 1
0
        // Begin downloading the file at the specified url, and save it to the given folder.
        void BeginDownload()
        {
            DownloadData data = null;
            FileStream   fs   = null;

            try
            {
                //start the stopwatch for speed calc
                sw.Start();

                // get download details
                waitingForResponse = true;
                data = DownloadData.Create(url, destFolder);
                waitingForResponse = false;

                //reset the adler
                downloadedAdler32.Reset();

                DownloadingTo = data.Filename;

                if (!File.Exists(DownloadingTo))
                {
                    // create the file
                    fs = File.Open(DownloadingTo, FileMode.Create, FileAccess.Write);
                }
                else
                {
                    // read in the existing data to calculate the adler32
                    if (Adler32 != 0)
                    {
                        GetAdler32(DownloadingTo);
                    }

                    // apend to an existing file (resume)
                    fs = File.Open(DownloadingTo, FileMode.Append, FileAccess.Write);
                }

                // create the download buffer
                byte[] buffer = new byte[BufferSize];

                int readCount;

                // update how many bytes have already been read
                sentSinceLastCalc = data.StartPoint; //for BPS calculation

                while ((readCount = data.DownloadStream.Read(buffer, 0, BufferSize)) > 0)
                {
                    // break on cancel
                    if (bw.CancellationPending)
                    {
                        data.Close();
                        fs.Close();
                        break;
                    }

                    // update total bytes read
                    data.StartPoint += readCount;

                    // update the adler32 value
                    if (Adler32 != 0)
                    {
                        downloadedAdler32.Update(buffer, 0, readCount);
                    }

                    // save block to end of file
                    fs.Write(buffer, 0, readCount);

                    //calculate download speed
                    calculateBps(data.StartPoint, data.TotalDownloadSize);

                    // send progress info
                    if (!bw.CancellationPending)
                    {
                        bw.ReportProgress(0, new object[] {
                            //use the realtive progress or the raw progress
                            UseRelativeProgress?
                            InstallUpdate.GetRelativeProgess(0, data.PercentDone) :
                                data.PercentDone,

                                // unweighted percent
                                data.PercentDone,
                                downloadSpeed, ProgressStatus.None, null
                        });
                    }

                    // break on cancel
                    if (bw.CancellationPending)
                    {
                        data.Close();
                        fs.Close();
                        break;
                    }
                }
            }
            catch (UriFormatException e)
            {
                throw new Exception(string.Format("Could not parse the URL \"{0}\" - it's either malformed or is an unknown protocol.", url), e);
            }
            catch (Exception e)
            {
                if (string.IsNullOrEmpty(DownloadingTo))
                {
                    throw new Exception(string.Format("Error trying to save file: {0}", e.Message), e);
                }
                else
                {
                    throw new Exception(string.Format("Error trying to save file \"{0}\": {1}", DownloadingTo, e.Message), e);
                }
            }
            finally
            {
                if (data != null)
                {
                    data.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
Ejemplo n.º 2
0
        private void BeginDownload()
        {
            DownloadData downloadData = null;
            FileStream   fileStream   = null;

            try
            {
                sw.Start();
                waitingForResponse = true;
                downloadData       = DownloadData.Create(url, destFolder);
                waitingForResponse = false;
                downloadedAdler32.Reset();
                DownloadingTo = downloadData.Filename;
                if (!File.Exists(DownloadingTo))
                {
                    fileStream = File.Open(DownloadingTo, FileMode.Create, FileAccess.Write);
                }
                else
                {
                    if (Adler32 != 0)
                    {
                        GetAdler32(DownloadingTo);
                    }
                    fileStream = File.Open(DownloadingTo, FileMode.Append, FileAccess.Write);
                }
                byte[] buffer = new byte[4096];
                sentSinceLastCalc = downloadData.StartPoint;
                do
                {
                    int num;
                    if ((num = downloadData.DownloadStream.Read(buffer, 0, 4096)) <= 0)
                    {
                        return;
                    }
                    if (bw.CancellationPending)
                    {
                        downloadData.Close();
                        fileStream.Close();
                        return;
                    }
                    downloadData.StartPoint += num;
                    if (Adler32 != 0)
                    {
                        downloadedAdler32.Update(buffer, 0, num);
                    }
                    fileStream.Write(buffer, 0, num);
                    calculateBps(downloadData.StartPoint, downloadData.TotalDownloadSize);
                    if (!bw.CancellationPending)
                    {
                        bw.ReportProgress(0, new object[5]
                        {
                            UseRelativeProgress?InstallUpdate.GetRelativeProgess(0, downloadData.PercentDone) : downloadData.PercentDone,
                                downloadData.PercentDone,
                                downloadSpeed,
                                ProgressStatus.None,
                                null
                        });
                    }
                }while (!bw.CancellationPending);
                downloadData.Close();
                fileStream.Close();
            }
            catch (UriFormatException innerException)
            {
                throw new Exception($"Could not parse the URL \"{url}\" - it's either malformed or is an unknown protocol.", innerException);
            }
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(DownloadingTo))
                {
                    throw new Exception($"Error trying to save file: {ex.Message}", ex);
                }
                throw new Exception($"Error trying to save file \"{DownloadingTo}\": {ex.Message}", ex);
            }
            finally
            {
                downloadData?.Close();
                fileStream?.Close();
            }
        }