Exemple #1
0
        public ChunkedFileStream(string path, long fileSize, ChunksData chunksData, HashFunction hashFunction,
                                 WorkFlags workFlags = WorkFlags.None)
        {
            Checks.ArgumentNotNullOrEmpty(path, "path");
            Checks.ArgumentMoreThanZero(fileSize, "fileSize");
            Checks.ArgumentNotNull(hashFunction, "hashFunction");

            DebugLogger.LogConstructor();
            DebugLogger.LogVariable(path, "path");
            DebugLogger.LogVariable(fileSize, "fileSize");

            _fileSize     = fileSize;
            _chunksData   = chunksData;
            _hashFunction = hashFunction;

            _buffer = new byte[_chunksData.ChunkSize];

            if ((workFlags | WorkFlags.PreservePreviousFile) != 0)
            {
                // Often you may want to continue downloading of a file if this exists
                // It tries to open a file and re-download it from the verified position.
                // It does not check the hash of the file. It trusts that the file is already valid up to that point.
                // Because the only way to download the file should be using Chunked Downloader.

                _fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
                _fileStream.Seek(0, SeekOrigin.End); // seek and stay at the end, so we can append
                long currentFileSize = _fileStream.Position;

                // Let's make sure that file size is a multiply of chunk size.
                // If not, something is wrong with the file.
                if (currentFileSize % chunksData.ChunkSize == 0)
                {
                    _chunkIndex = (int)(currentFileSize / chunksData.ChunkSize);
                }
                else
                {
                    DebugLogger.LogWarningFormat(
                        "File {0} size {1} is not a multiply of chunk size: {2}. Will recreate it.", path,
                        currentFileSize, chunksData.ChunkSize);

                    _fileStream.Close();
                    _fileStream.Dispose();

                    _fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
                }
            }
            else
            {
                _fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
            }
        }
Exemple #2
0
        public ChunkedHttpDownloader(string destinationFilePath, RemoteResource resource, int timeout)
        {
            Checks.ArgumentParentDirectoryExists(destinationFilePath, "destinationFilePath");
            Checks.ArgumentValidRemoteResource(resource, "resource");
            Checks.ArgumentMoreThanZero(timeout, "timeout");

            DebugLogger.LogConstructor();
            DebugLogger.LogVariable(destinationFilePath, "destinationFilePath");
            DebugLogger.LogVariable(resource, "resource");
            DebugLogger.LogVariable(timeout, "timeout");

            _destinationFilePath = destinationFilePath;
            _resource            = resource;
            _timeout             = timeout;
        }
Exemple #3
0
        public HttpDownloader(string destinationFilePath, string[] mirrorUrls, long size, int timeout)
        {
            Checks.ArgumentParentDirectoryExists(destinationFilePath, "destinationFilePath");
            Checks.ArgumentMoreThanZero(timeout, "timeout");
            Checks.ArgumentNotNull(mirrorUrls, "mirrorUrls");

            DebugLogger.LogConstructor();
            DebugLogger.LogVariable(destinationFilePath, "destinationFilePath");
            DebugLogger.LogVariable(mirrorUrls, "mirrorUrls");
            DebugLogger.LogVariable(size, "size");
            DebugLogger.LogVariable(timeout, "timeout");

            _destinationFilePath = destinationFilePath;
            _mirrorUrls          = mirrorUrls;
            _size    = size;
            _timeout = timeout;
        }
        public BaseHttpDownloader(string url, int timeout, int bufferSize,
                                  CreateNewHttpWebRequest createNewHttpWebRequest)
        {
            Checks.ArgumentNotNullOrEmpty(url, "url");
            Checks.ArgumentMoreThanZero(timeout, "timeout");
            Checks.ArgumentMoreThanZero(bufferSize, "bufferSize");
            Checks.ArgumentNotNull(createNewHttpWebRequest, "createNewHttpWebRequest");

            DebugLogger.LogConstructor();
            DebugLogger.LogVariable(url, "url");
            DebugLogger.LogVariable(timeout, "timeout");
            DebugLogger.LogVariable(bufferSize, "bufferSize");

            _url        = url;
            _timeout    = timeout;
            _bufferSize = bufferSize;
            _createNewHttpWebRequest = createNewHttpWebRequest;
            _buffer = new byte[_bufferSize];

            ServicePointManager.ServerCertificateValidationCallback =
                (sender, certificate, chain, errors) => true;
            ServicePointManager.DefaultConnectionLimit = 65535;
        }