@Override
  public void handleFree(final DownloadLink downloadLink) throws Exception {
    final LinkStatus linkStatus = downloadLink.getLinkStatus();

    this.dl =
        jd.plugins.BrowserAdapter.openDownload(
            this.br, downloadLink, downloadLink.getDownloadURL());
    URLConnectionAdapter urlConnection = this.dl.connect();
    if (urlConnection.getLongContentLength() == 0) {
      this.br.followConnection();
      linkStatus.addStatus(LinkStatus.ERROR_PLUGIN_DEFECT);
      return;
    }

    if (this.dl.startDownload()) {
      if (downloadLink.getProperty("convertto") != null) {
        final DestinationFormat convertTo =
            DestinationFormat.valueOf(downloadLink.getProperty("convertto").toString());
        final DestinationFormat inType = DestinationFormat.VIDEOFLV;
        /* to load the TbCm plugin */
        JDUtilities.getPluginForDecrypt("youtube.com");
        if (!TbCm.ConvertFile(downloadLink, inType, convertTo)) {
          Plugin.logger.severe("Video-Convert failed!");
        }
      }
    }
  }
 /** fill given DownloadInformations with current details of this DownloadController */
 protected void getDownloadStatus(final DownloadInformations ds) {
   ds.reset();
   ds.addRunningDownloads(DownloadWatchDog.getInstance().getActiveDownloads());
   final boolean readL = readLock();
   try {
     LinkStatus linkStatus;
     boolean isEnabled;
     for (final FilePackage fp : packages) {
       synchronized (fp) {
         ds.addPackages(1);
         ds.addDownloadLinks(fp.size());
         for (final DownloadLink l : fp.getChildren()) {
           linkStatus = l.getLinkStatus();
           isEnabled = l.isEnabled();
           if (!linkStatus.hasStatus(LinkStatus.ERROR_ALREADYEXISTS) && isEnabled) {
             ds.addTotalDownloadSize(l.getDownloadSize());
             ds.addCurrentDownloadSize(l.getDownloadCurrent());
           }
           if (linkStatus.hasStatus(LinkStatus.ERROR_ALREADYEXISTS)) {
             ds.addDuplicateDownloads(1);
           } else if (!isEnabled) {
             ds.addDisabledDownloads(1);
           } else if (linkStatus.hasStatus(LinkStatus.FINISHED)) {
             ds.addFinishedDownloads(1);
           }
         }
       }
     }
   } finally {
     readUnlock(readL);
   }
 }
    /**
     * ueber error() kann ein fehler gemeldet werden. DIe Methode entscheided dann ob dieser fehler zu einem Abbruch fuehren muss
     */
    protected synchronized void error(int id, String string) {
        /* if we recieved external stop, then we dont have to handle errors */
        if (externalDownloadStop()) return;

        logger.severe("Error occured (" + id + "): ");

        if (errors.indexOf(id) < 0) errors.add(id);
        if (fatalErrorOccured) return;

        linkStatus.addStatus(id);

        linkStatus.setErrorMessage(string);
        switch (id) {
        case LinkStatus.ERROR_RETRY:
        case LinkStatus.ERROR_FATAL:
        case LinkStatus.ERROR_TIMEOUT_REACHED:
        case LinkStatus.ERROR_FILE_NOT_FOUND:
        case LinkStatus.ERROR_TEMPORARILY_UNAVAILABLE:
        case LinkStatus.ERROR_LOCAL_IO:
        case LinkStatus.ERROR_NO_CONNECTION:
        case LinkStatus.ERROR_ALREADYEXISTS:
        case LinkStatus.ERROR_DOWNLOAD_FAILED:
            fatalErrorOccured = true;
            terminate();
        }
    }
    /**
     * Setzt im Downloadlink und PLugin die entsprechende Fehlerids
     */
    public boolean handleErrors() {
        if (externalDownloadStop()) return false;
        if (doFilesizeCheck() && (totalLinkBytesLoaded <= 0 || totalLinkBytesLoaded != getFileSize() && getFileSize() > 0)) {
            if (totalLinkBytesLoaded > getFileSize()) {
                /*
                 * workaround for old bug deep in this downloadsystem. more data got loaded (maybe just counting bug) than filesize. but in
                 * most cases the file is okay! WONTFIX because new downloadsystem is on its way
                 */
                logger.severe("Filesize: " + getFileSize() + " Loaded: " + totalLinkBytesLoaded);
                if (!linkStatus.isFailed()) {
                    linkStatus.setStatus(LinkStatus.FINISHED);
                }
                return true;
            }
            logger.severe("Filesize: " + getFileSize() + " Loaded: " + totalLinkBytesLoaded);
            logger.severe("DOWNLOAD INCOMPLETE DUE TO FILESIZECHECK");
            error(LinkStatus.ERROR_DOWNLOAD_INCOMPLETE, _JDT._.download_error_message_incomplete());
            return false;
        }

        if (getExceptions() != null && getExceptions().size() > 0) {
            error(LinkStatus.ERROR_RETRY, _JDT._.download_error_message_incomplete());
            return false;
        }
        if (!linkStatus.isFailed()) {
            linkStatus.setStatus(LinkStatus.FINISHED);
        }
        return true;
    }
 public OldRAFDownload(PluginForHost plugin, DownloadLink downloadLink, Request request) throws IOException, PluginException {
     downloadLink.setDownloadInstance(this);
     connectionHandler = new ManagedThrottledConnectionHandler(downloadLink);
     this.downloadLink = downloadLink;
     this.plugin = plugin;
     logger = plugin.getLogger();
     linkStatus = downloadLink.getLinkStatus();
     linkStatus.setStatusText(_JDT._.download_connection_normal());
     browser = plugin.getBrowser().cloneBrowser();
     requestTimeout = JsonConfig.create(GeneralSettings.class).getHttpConnectTimeout();
     readTimeout = JsonConfig.create(GeneralSettings.class).getHttpReadTimeout();
     this.request = request;
 }
 /**
  * Startet den Download. Nach dem Aufruf dieser Funktion koennen keine Downlaodparameter mehr gesetzt werden bzw bleiben wirkungslos.
  * 
  * @return
  * @throws Exception
  */
 public boolean startDownload() throws Exception {
     try {
         startTimeStamp = System.currentTimeMillis();
         sizeBefore = downloadLink.getDownloadCurrent();
         linkStatus.addStatus(LinkStatus.DOWNLOADINTERFACE_IN_PROGRESS);
         try {
             downloadLink.getDownloadLinkController().getConnectionHandler().addConnectionHandler(this.getManagedConnetionHandler());
         } catch (final Throwable e) {
         }
         /*if (this.dlAlreadyFinished == false) {
             try {
                 boolean watchAsYouDownloadTypeLink = (Boolean) downloadLink.getFilePackage().getProperty(org.jdownloader.extensions.neembuu.NeembuuExtension.WATCH_AS_YOU_DOWNLOAD_KEY, false);
                 boolean initiatedByWatchAsYouDownloadAction = (Boolean) downloadLink.getFilePackage().getProperty(org.jdownloader.extensions.neembuu.NeembuuExtension.INITIATED_BY_WATCH_ACTION, false);
                 if (watchAsYouDownloadTypeLink && initiatedByWatchAsYouDownloadAction) {
                     org.jdownloader.extensions.neembuu.DownloadSession downloadSession = new org.jdownloader.extensions.neembuu.DownloadSession(downloadLink, this, this.plugin, this.getConnection(), this.browser.cloneBrowser());
                     if (org.jdownloader.extensions.neembuu.NeembuuExtension.tryHandle(downloadSession)) {
                         org.jdownloader.extensions.neembuu.WatchAsYouDownloadSession watchAsYouDownloadSession = downloadSession.getWatchAsYouDownloadSession();
                         try {
                             watchAsYouDownloadSession.waitForDownloadToFinish();
                         } catch (Exception a) {
                             logger.log(Level.SEVERE, "Exception in waiting for neembuu watch as you download", a);
                             // if we do not return, normal download would start.
                             return false;
                         }
                         return true;
                     }
                     int o = 0;
                     try {
                         o = Dialog.I().showConfirmDialog(Dialog.LOGIC_COUNTDOWN, org.jdownloader.extensions.neembuu.translate._NT._.neembuu_could_not_handle_title(), org.jdownloader.extensions.neembuu.translate._NT._.neembuu_could_not_handle_message());
                     } catch (Exception a) {
                         o = Dialog.RETURN_CANCEL;
                     }
                     if (o == Dialog.RETURN_CANCEL) return false;
                     logger.severe("Neembuu could not handle this link/filehost. Using default download system.");
                 } else if (watchAsYouDownloadTypeLink && !initiatedByWatchAsYouDownloadAction) {
                     // Neembuu downloads should start if and only if user clicks
                     // watch as you download
                     // action in the context menu. We don't want neembuu to
                     // start when user pressed
                     // forced download, or simple download button.
                     // We shall skip this link and disable all in this
                     // filepackage
                     for (DownloadLink dl : downloadLink.getFilePackage().getChildren()) {
                         dl.setEnabled(false);
                     }
                     // TODO: change me 
                     throw new PluginException(LinkStatus.ERROR_FATAL);
                 }
             } catch (final Throwable e) {
                 logger.severe("Exception in neembuu watch as you download");
                 LogSource.exception(logger, e);
             }
         }*/
         logger.finer("Start Download");
         if (this.dlAlreadyFinished == true) {
             downloadLink.setAvailable(true);
             logger.finer("DownloadAlreadyFinished workaround");
             linkStatus.setStatus(LinkStatus.FINISHED);
             return true;
         }
         if (!connected) connect();
         if (connection != null && connection.getHeaderField("Content-Encoding") != null && connection.getHeaderField("Content-Encoding").equalsIgnoreCase("gzip")) {
             /* GZIP Encoding kann weder chunk noch resume */
             setResume(false);
             setChunkNum(1);
         }
         // Erst hier Dateinamen holen, somit umgeht man das Problem das bei
         // mehrfachAufruf von connect entstehen kann
         if (this.downloadLink.getFinalFileName() == null && ((connection != null && connection.isContentDisposition()) || this.allowFilenameFromURL)) {
             String name = Plugin.getFileNameFromHeader(connection);
             if (this.fixWrongContentDispositionHeader) {
                 this.downloadLink.setFinalFileName(Encoding.htmlDecode(name));
             } else {
                 this.downloadLink.setFinalFileName(name);
             }
         }
         downloadLink.getLinkStatus().setStatusText(null);
         if (connection == null || !connection.isOK()) {
             if (connection != null) logger.finest(connection.toString());
             try {
                 connection.disconnect();
             } catch (final Throwable e) {
             }
             throw new PluginException(LinkStatus.ERROR_TEMPORARILY_UNAVAILABLE, 10 * 60 * 1000l);
         }
         if (connection.getHeaderField("Location") != null) {
             error(LinkStatus.ERROR_PLUGIN_DEFECT, "Sent a redirect to Downloadinterface");
             return false;
         }
         if (downloadLink.getVerifiedFileSize() < 0) {
             /* we don't have a verified filesize yet, let's check if we have it now! */
             if (connection.getRange() != null) {
                 if (connection.getRange()[2] > 0) {
                     downloadLink.setVerifiedFileSize(connection.getRange()[2]);
                 }
             } else if (connection.getRequestProperty("Range") == null && connection.getLongContentLength() > 0 && connection.isOK()) {
                 downloadLink.setVerifiedFileSize(connection.getLongContentLength());
             }
         }
         //if (DownloadWatchDog.preDownloadCheckFailed(downloadLink)) return false;
         setupChunks();
         /* download in progress so file should be online ;) */
         downloadLink.setAvailable(true);
         waitForChunks();
         onChunksReady();
         return handleErrors();
     } catch (Exception e) {
         if (e instanceof FileNotFoundException) {
             this.error(LinkStatus.ERROR_LOCAL_IO, _JDT._.download_error_message_localio(e.getMessage()));
         } else {
             LogSource.exception(logger, e);
         }
         handleErrors();
         return false;
     } finally {
         try {
             downloadLink.getDownloadLinkController().getConnectionHandler().removeConnectionHandler(this.getManagedConnetionHandler());
         } catch (final Throwable e) {
         }
         linkStatus.removeStatus(LinkStatus.DOWNLOADINTERFACE_IN_PROGRESS);
         try {
             this.connection.disconnect();
         } catch (Throwable e) {
         }
         /* make sure file is really closed */
         cleanupDownladInterface();
     }
 }