Attempts a resumable download.

        Raises ResumableDownloadException if any problems occur.
961236
def _attempt_resumable_download(self, key, fp, headers, cb, num_cb,
                                    torrent, version_id):
        
        cur_file_size = get_cur_file_size(fp, position_to_eof=True)

        if (cur_file_size and 
            self.etag_value_for_current_download and
            self.etag_value_for_current_download == key.etag.strip('"\'')):
            # Try to resume existing transfer.
            if cur_file_size > key.size:
              raise ResumableDownloadException(
                  '%s is larger (%d) than %s (%d).\nDeleting tracker file, so '
                  'if you re-try this download it will start from scratch' %
                  (fp.name, cur_file_size, str(storage_uri_for_key(key)),
                   key.size), ResumableTransferDisposition.ABORT)
            elif cur_file_size == key.size:
                if key.bucket.connection.debug >= 1:
                    print 'Download complete.'
                return
            if key.bucket.connection.debug >= 1:
                print 'Resuming download.'
            headers = headers.copy()
            headers['Range'] = 'bytes=%d-%d' % (cur_file_size, key.size - 1)
            cb = ByteTranslatingCallbackHandler(cb, cur_file_size).call
            self.download_start_point = cur_file_size
        else:
            if key.bucket.connection.debug >= 1:
                print 'Starting new resumable download.'
            self._save_tracker_info(key)
            self.download_start_point = 0
            # Truncate the file, in case a new resumable download is being
            # started atop an existing file.
            fp.truncate(0)

        # Disable AWSAuthConnection-level retry behavior, since that would
        # cause downloads to restart from scratch.
        key.get_file(fp, headers, cb, num_cb, torrent, version_id,
                     override_num_retries=0)
        fp.flush()
1187628
private void handleReopenForStorageException(final StorageException exs) throws StorageException {
    reopens++;
    if (reopens > maxReopens) {
      throw new StorageException(
          exs.getCode(),
          "All "
              + maxReopens
              + " reopens failed. Waited a total of "
              + totalWaitTime
              + " ms between attempts",
          exs);
    }
    sleepForAttempt(reopens);
  }
207199
private void checkResumableDownloadEnabled(int downloadId) {
		synchronized (mCurrentRequests) {
			for (DownloadRequest request : mCurrentRequests) {
				if (downloadId == -1 && !request.isResumable()) {
					Log.e("ThinDownloadManager",
							String.format(Locale.getDefault(), "This request has not enabled resume feature hence request will be cancelled. Request Id: %d", request.getDownloadId()));
				} else if ((request.getDownloadId() == downloadId && !request.isResumable())) {
					throw new IllegalStateException("You cannot pause the download, unless you have enabled Resume feature in DownloadRequest.");
				} else {
					//ignored, It can not be a scenario to happen.
				}
			}
		}
	}
123502
def is_error_retryable(request, exc):
    
    if is_last_attempt(request):
        return False

    return (
        isinstance(exc, RetryableException)
        or IRetryableError.providedBy(exc)
    )
237840
@Override
    public File call() throws Exception {
        try {
            latch.await();

            if (isTimeoutEnabled()) {
                timedExecutor.schedule(new Runnable() {
                    public void run() {
                        try {
                            if (abortableDownload.getState() != Transfer.TransferState.Completed) {
                                abortableDownload.abort();
                            }
                        } catch (Exception e) {
                            throw new SdkClientException(
                                "Unable to abort download after timeout", e);
                        }
                    }
                }, timeout, TimeUnit.MILLISECONDS);
            }

            setState(Transfer.TransferState.InProgress);
            createParentDirectoryIfNecessary(dstfile);

            if (isDownloadParallel) {
                downloadInParallel();
            } else {
                downloadAsSingleObject();
            }
            return dstfile;
        } catch (Throwable t) {
            cleanupAfterException();
            if (t instanceof Exception) {
                throw (Exception) t;
            } else {
                throw (Error) t;
            }
        }
    }
852216
def _download(self, force_overwrite=False, verbose=False):
        
        if not force_overwrite:
            # If the file is already there, we're done
            if os.path.isfile(self.filepath):
                if verbose:
                    print(
                        "File already available at %s -- skipping"
                        % (self.filepath)
                    )
                return False
        stream_download(self.URL, self.filepath, verbose=verbose)
        return True
509825
def download_file_part_run(download_context):
    
    destination_dir, file_url_data_dict, seek_amt, bytes_to_read = download_context.params
    project_file = ProjectFile(file_url_data_dict)
    local_path = project_file.get_local_path(destination_dir)
    retry_chunk_downloader = RetryChunkDownloader(project_file, local_path,
                                                  seek_amt, bytes_to_read,
                                                  download_context)
    retry_chunk_downloader.run()
    return 'ok'
660181
function attemptToDownload() {
        if (checkIfOnline()) {
            postMessageToNode(MessageIds.PERFORM_CLEANUP, ['.json'], true);
        } else {
            enableCheckForUpdateEntry(true);
            UpdateStatus.cleanUpdateStatus();
            HealthLogger.sendAnalyticsData(
                autoUpdateEventNames.AUTOUPDATE_DOWNLOAD_FAILED,
                "autoUpdate",
                "download",
                "fail",
                "No Internet connection available."
            );
            UpdateInfoBar.showUpdateBar({
                type: "warning",
                title: Strings.DOWNLOAD_FAILED,
                description: Strings.INTERNET_UNAVAILABLE
            });

            setUpdateStateInJSON("autoUpdateInProgress", false);
        }
    }
918841
def RetryOnUnavailable(func):
  """"""

  @functools.wraps(func)
  def Wrapper(*args, **kwargs):
    while True:
      try:
        response = func(*args, **kwargs)
      except (httpclient.HTTPException, socket.error, urlerror.URLError) as e:
        time.sleep(5)
        if (isinstance(e, urlerror.HTTPError)
            and e.getcode() == httpclient.SERVICE_UNAVAILABLE):
          continue
        elif isinstance(e, socket.timeout):
          continue
        raise
      else:
        if response.getcode() == httpclient.OK:
          return response
        else:
          raise StatusException(response)
  return Wrapper
588348
@Override
    public File call() throws Exception {
        try {
            latch.await();

            if (isTimeoutEnabled()) {
                timedExecutor.schedule(new Runnable() {
                    public void run() {
                        try {
                            if (download.getState() != TransferState.Completed) {
                                download.abort();
                            }
                        } catch (Exception e) {
                            throw new SdkClientException(
                                "Unable to abort download after timeout", e);
                        }
                    }
                }, timeout, TimeUnit.MILLISECONDS);
            }

            download.setState(TransferState.InProgress);
            createParentDirectoryIfNecessary(dstfile);

            if (isDownloadParallel) {
                downloadInParallel(ServiceUtils.getPartCount(req, s3));
            } else {
                S3Object s3Object = retryableDownloadS3ObjectToFile(dstfile,
                                                                    new DownloadTaskImpl(s3, download, req));
                updateDownloadStatus(s3Object);
            }
            return dstfile;
        } catch (Throwable t) {
            // Cancel all the futures
            for (Future<Long> f : futures) {
                f.cancel(true);
            }
            // Downloads aren't allowed to move from canceled to failed
            if (download.getState() != TransferState.Canceled) {
                download.setState(TransferState.Failed);
            }
            if (t instanceof Exception)
                throw (Exception) t;
            else
                throw (Error) t;
        }
    }
203641
private synchronized void handleUnavailableException(UnavailableException e, boolean isInit) throws UnavailableException
	 {
        if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled() && logger.isLoggable(Level.FINE)) // 306998.15
            logger.entering(CLASS_NAME, "handleUnavailableException", e); // 569469
        // logger.logp(Level.FINE, CLASS_NAME,"handleUnavailableException", "",
        // e);
        if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled() && logger.isLoggable(Level.FINE)) // 306998.15
            logger.logp(Level.FINE, CLASS_NAME, "handleUnavailableException", "UnavailableException was thrown by servlet: " + getServletName()
                    + " reason:" + e.getMessage());
        if (state == UNAVAILABLE_PERMANENTLY_STATE) {
            if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled() && logger.isLoggable(Level.FINE)) // 306998.15
                logger.exiting(CLASS_NAME, "handleUnavailableException", "state is already permanently unavailable, throw ue"); // 569469
            // logger.logp(Level.FINE, CLASS_NAME,"handleUnavailableException",
            // "state is already permanently unavailable, throw ue");
            throw new UnavailableException(unavailableMessage);
        } else if (e.isPermanent()) {
            if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled() && logger.isLoggable(Level.FINE)) // 306998.15
                logger.logp(Level.FINE, CLASS_NAME, "handleUnavailableException", "exception is permanent");
            if (state == AVAILABLE_STATE) {
                if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled() && logger.isLoggable(Level.FINE)) // 306998.15
                    logger.logp(Level.FINE, CLASS_NAME, "handleUnavailableException", "state is available so destroy the servlet");
                /*
                 * This means the UnavailableException is thrown from the
                 * service method so we should destroy this servlet.
                 */
                // destroy();
                // destroy does not remove from the requestMapper which can lead
                // to a 500 error code on a subsequent request
                if (!context.removeServlet(getServletName()))
                    ;
                {
                    // In the case that there are no servlet mappings, we still
                    // need to destroy
                    if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled() && logger.isLoggable(Level.FINE)) // 306998.15
                        logger.logp(Level.FINE, CLASS_NAME, "handleUnavailableException",
                                "removeServlet didn't destroy. destroy from handleUnavailableException");
                    destroy();
                }
            }
            unavailableMessage = e.getMessage();
            if(isInit){											
				this.setUninitialize();  // PM01373
			}
			else{				
            setUnavailable();
			}
        } else {
            int secs = e.getUnavailableSeconds();
            if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled() && logger.isLoggable(Level.FINE)) // 306998.15
                logger.logp(Level.FINE, CLASS_NAME, "handleUnavailableException", "ue is not permanent, unavailable secs -->[" + secs + "]");
            if (secs > 0) {
                long time = System.currentTimeMillis() + ((secs) * 1000);
                if (time > unavailableUntil) {
                    unavailableMessage = e.getMessage();
                    // only set the time if the new time is farther away than
                    // the old time.
                    setUnavailableUntil(time, isInit);		//PM01373
                }
            }
        }
        if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled() && logger.isLoggable(Level.FINE)) // 306998.15
            logger.exiting(CLASS_NAME, "handleUnavailableException"); // 569469
    }
958418
private Object retry4ConfDownload(RemoteUrl remoteUrl, File localTmpFile, int retryTimes, int sleepSeconds)
            throws Exception {
        Exception ex = null;
        for (URL url : remoteUrl.getUrls()) {

            // 可重试的下载
            UnreliableInterface unreliableImpl = new FetchConfFile(url, localTmpFile);

            try {

                return retryStrategy.retry(unreliableImpl, retryTimes, sleepSeconds);

            } catch (Exception e) {
                ex = e;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    LOGGER.info("pass");
                }
            }
        }

        throw new Exception("download failed.", ex);
    }
926474
def choice_download(self):
        
        Download(path="", url=self.dwn_srcs, repo="sbo").start()
        raise SystemExit()
462615
def __StreamMedia(self, callback=None, finish_callback=None,
                      additional_headers=None, use_chunks=True):
        """"""
        if self.strategy != RESUMABLE_UPLOAD:
            raise exceptions.InvalidUserInputError(
                'Cannot stream non-resumable upload')
        callback = callback or self.progress_callback
        finish_callback = finish_callback or self.finish_callback
        # final_response is set if we resumed an already-completed upload.
        response = self.__final_response

        def CallSendChunk(start):
            return self.__SendChunk(
                start, additional_headers=additional_headers)

        def CallSendMediaBody(start):
            return self.__SendMediaBody(
                start, additional_headers=additional_headers)

        send_func = CallSendChunk if use_chunks else CallSendMediaBody
        if not use_chunks and self.__gzip_encoded:
            raise exceptions.InvalidUserInputError(
                'Cannot gzip encode non-chunked upload')
        if use_chunks:
            self.__ValidateChunksize(self.chunksize)
        self.EnsureInitialized()
        while not self.complete:
            response = send_func(self.stream.tell())
            if response.status_code in (http_client.OK, http_client.CREATED):
                self.__complete = True
                break
            if response.status_code not in (
                    http_client.OK, http_client.CREATED,
                    http_wrapper.RESUME_INCOMPLETE):
                # Only raise an exception if the error is something we can't
                # recover from.
                if (self.strategy != RESUMABLE_UPLOAD or
                        not self.__IsRetryable(response)):
                    raise exceptions.HttpError.FromResponse(response)
                # We want to reset our state to wherever the server left us
                # before this failed request, and then raise.
                self.RefreshResumableUploadState()

                self._ExecuteCallback(callback, response)
                continue

            self.__progress = self.__GetLastByte(
                self._GetRangeHeaderFromResponse(response))
            if self.progress + 1 != self.stream.tell():
                # TODO(craigcitro): Add a better way to recover here.
                raise exceptions.CommunicationError(
                    'Failed to transfer all bytes in chunk, upload paused at '
                    'byte %d' % self.progress)
            self._ExecuteCallback(callback, response)
        if self.__complete and hasattr(self.stream, 'seek'):
            current_pos = self.stream.tell()
            self.stream.seek(0, os.SEEK_END)
            end_pos = self.stream.tell()
            self.stream.seek(current_pos)
            if current_pos != end_pos:
                raise exceptions.TransferInvalidError(
                    'Upload complete with %s additional bytes left in stream' %
                    (int(end_pos) - int(current_pos)))
        self._ExecuteCallback(finish_callback, response)
        return response
1173804
public function download() {
        // check if the file extension is allowed
        $this->validateExtension();       
        
        // download the file
        $this->downloadFile();
        
        // check the mime type for the file, if it is of the wrong type, unset the
        // file and throw the error on
        try {
            $this->validateFileType();
        } catch (Exception $exception) {
            // remove the file, the mime type is not allowed
            unlink($this->downloadFilePath());
            
            // throw the error on
            throw $exception;
        }

        // everything ok
        return true;
    }
691314
public void download() throws IOException, InterruptedException {
    if (!Files.exists(destinationFile.getParent())) {
      Files.createDirectories(destinationFile.getParent());
    }

    if (Files.exists(destinationFile)) {
      throw new FileAlreadyExistsException(destinationFile.toString());
    }
    URLConnection connection = address.openConnection();
    connection.setRequestProperty("User-Agent", userAgentString);

    try (InputStream in = connection.getInputStream()) {
      // note : contentLength can potentially be -1 if it is unknown.
      long contentLength = connection.getContentLengthLong();

      logger.info("Downloading " + address + " to " + destinationFile);

      try (BufferedOutputStream out =
          new BufferedOutputStream(
              Files.newOutputStream(destinationFile, StandardOpenOption.CREATE_NEW))) {

        progressListener.start(
            getDownloadStatus(contentLength, Locale.getDefault()), contentLength);

        int bytesRead;
        byte[] buffer = new byte[BUFFER_SIZE];

        while ((bytesRead = in.read(buffer)) != -1) {
          if (Thread.currentThread().isInterrupted()) {
            logger.warning("Download was interrupted\n");
            cleanUp();
            throw new InterruptedException("Download was interrupted");
          }

          out.write(buffer, 0, bytesRead);
          progressListener.update(bytesRead);
        }
      }
    }
    progressListener.done();
  }
596035
void attemptRestoreRemovedStorage() {
    // if directory is "alive" - copy the images there...
    if(removedStorageDirs.size() == 0)
      return; //nothing to restore

    /* We don't want more than one thread trying to restore at a time */
    synchronized (this.restorationLock) {
      LOG.info("attemptRestoreRemovedStorage: check removed(failed) "+
               "storage. removedStorages size = " + removedStorageDirs.size());
      for(Iterator<StorageDirectory> it
            = this.removedStorageDirs.iterator(); it.hasNext();) {
        StorageDirectory sd = it.next();
        File root = sd.getRoot();
        LOG.info("attemptRestoreRemovedStorage: currently disabled dir "
            + root.getAbsolutePath()
            + "; type="
            + sd.getStorageDirType()
            + ";canwrite=" + root.canWrite());
        try {
          
          if(root.exists() && root.canWrite()) { 
            LOG.info("attemptRestoreRemovedStorage: restoring dir "
                + sd.getRoot().getAbsolutePath());
            this.addStorageDir(sd); // restore
            it.remove();
            sd.lock();
          }
        } catch(IOException e) {
          LOG.warn("attemptRestoreRemovedStorage: failed to restore "
              + sd.getRoot().getAbsolutePath(), e);
        }
      }
    }
  }