private IStatus runSafely(IProgressMonitor monitor) throws Exception {
  monitor.beginTask(null,100);
  monitor.subTask(NLS.bind(Messages.ConnectingSource,getSourceURL()));
  URL url=new URL(sourceURL);
  URLConnection connection=url.openConnection();
  setURLConnection(connection);
  if (monitor.isCanceled())   return cancelStatus();
  setupConnection(connection);
  if (monitor.isCanceled())   return cancelStatus();
  connection.connect();
  if (monitor.isCanceled())   return cancelStatus();
  IStatus consumed=validateConnection(connection);
  if (consumed != null)   return consumed;
  if (monitor.isCanceled())   return cancelStatus();
  int length=connection.getContentLength();
  if (monitor.isCanceled())   return cancelStatus();
  InputStream sourceStream=connection.getInputStream();
  try {
    if (monitor.isCanceled())     return cancelStatus();
    monitor.subTask(NLS.bind(Messages.InitializingTarget,getTargetPath()));
    OutputStream targetStream=target.openOutputStream();
    if (monitor.isCanceled())     return cancelStatus();
    try {
      sourceStream=new MonitoredInputStream(sourceStream,monitor);
      targetStream=new MonitoredOutputStream(targetStream,monitor);
      monitor.subTask(Messages.TransferingData);
      transfer(sourceStream,targetStream,new SubProgressMonitor(monitor,100),length);
      setURLConnection(null);
      monitor.done();
      return new Status(IStatus.OK,pluginId,NLS.bind(Messages.DownloadFinished,getSourceURL(),getTargetPath()));
    }
  finally {
      try {
        targetStream.close();
      }
 catch (      IOException ignore) {
      }
    }
  }
  finally {
    try {
      sourceStream.close();
    }
 catch (    IOException ignore) {
    }
  }
}

private IStatus runSafely ( IProgressMonitor monitor ) throws Exception { monitor . beginTask ( null , 100 ) ; monitor . subTask ( NLS . bind ( Messages . ConnectingSource , getSourceURL ( ) ) ) ; URL url = new URL ( sourceURL ) ; URLConnection connection = url . openConnection ( ) ; setURLConnection ( connection ) ; if ( monitor . isCanceled ( ) ) return cancelStatus ( ) ; setupConnection ( connection ) ; if ( monitor . isCanceled ( ) ) return cancelStatus ( ) ; connection . connect ( ) ; if ( monitor . isCanceled ( ) ) return cancelStatus ( ) ; IStatus consumed = validateConnection ( connection ) ; if ( consumed != null ) return consumed ; if ( monitor . isCanceled ( ) ) return cancelStatus ( ) ; int length = connection . getContentLength ( ) ; if ( monitor . isCanceled ( ) ) return cancelStatus ( ) ; InputStream sourceStream = connection . getInputStream ( ) ; try { if ( monitor . isCanceled ( ) ) return cancelStatus ( ) ; monitor . subTask ( NLS . bind ( Messages . InitializingTarget , getTargetPath ( ) ) ) ; OutputStream targetStream = target . openOutputStream ( ) ; if ( monitor . isCanceled ( ) ) return cancelStatus ( ) ; try { sourceStream = new MonitoredInputStream ( sourceStream , monitor ) ; targetStream = new MonitoredOutputStream ( targetStream , monitor ) ; monitor . subTask ( Messages . TransferingData ) ; transfer ( sourceStream , targetStream , new SubProgressMonitor ( monitor , 100 ) , length ) ; setURLConnection ( null ) ; monitor . done ( ) ; return new Status ( IStatus . OK , pluginId , NLS . bind ( Messages . DownloadFinished , getSourceURL ( ) , getTargetPath ( ) ) ) ; } finally { try { targetStream . close ( ) ; } catch ( IOException ignore ) { } } } finally { try { sourceStream . close ( ) ; } catch ( IOException ignore ) { } } }
executes the download job in a safe context . it ' s safe to throw exceptions in this method to interrupt the download process .
