@Override
 public void pause() {
   if (state != TransferState.FINISHED) {
     state = TransferState.CANCELING;
     httpClient.cancel();
   }
 }
  YouTubeDownload(TransferManager manager, YouTubeCrawledSearchResult sr) {
    this.manager = manager;
    this.sr = sr;
    this.downloadType = buildDownloadType(sr);
    this.size = sr.getSize();

    String filename = sr.getFilename();

    File savePath = SystemPaths.getTorrentData();

    ensureDirectoryExits(savePath);
    ensureDirectoryExits(SystemPaths.getTemp());

    completeFile = buildFile(savePath, filename);
    tempVideo = buildTempFile(FilenameUtils.getBaseName(filename), "m4v");
    tempAudio = buildTempFile(FilenameUtils.getBaseName(filename), "m4a");

    bytesReceived = 0;
    dateCreated = new Date();

    httpClientListener = new HttpDownloadListenerImpl();

    httpClient = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.DOWNLOAD);
    httpClient.setListener(httpClientListener);

    if (TransferManager.isCurrentMountAlmostFull()) {
      this.status = STATUS_ERROR_DISK_FULL;
    }
  }
  @Override
  public void remove() {
    if (state != TransferState.FINISHED) {
      state = TransferState.CANCELING;
      httpClient.cancel();
    }

    if (deleteDataWhenRemoved) {
      getSaveLocation().delete();
    }
  }
  private YouTubeSig getYouTubeSig(String html5playerUrl) {
    // concurrency issues are not important in this point
    YouTubeSig sig = null;
    if (!YT_SIG_MAP.containsKey(html5playerUrl)) {
      String jscode = "";
      try {
        html5playerUrl = html5playerUrl.replace("\\", "");
        HttpClient httpClient = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.SEARCH);
        jscode = httpClient.get(html5playerUrl);
        sig = new YouTubeSig(jscode);
        YT_SIG_MAP.put(html5playerUrl, sig);
      } catch (Throwable t) {
        LOG.error("Could not getYouTubeSig", t);
        LOG.error("jscode:\n" + jscode);
      }
    } else {
      // cache hit, it worked with this url.
      sig = YT_SIG_MAP.get(html5playerUrl);
    }

    return sig;
  }
  @LargeTest
  public void testDownload1() throws IOException {

    HttpClient c = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.MISC);

    File torrentFile =
        new File(SystemUtils.getTorrentsDirectory(), "create_download_test1.torrent");
    File saveDir = SystemUtils.getTorrentDataDirectory();
    c.save(TorrentUrls.FROSTCLICK_BRANDON_HINES_2010, torrentFile);

    final CountDownLatch signal = new CountDownLatch(1);

    VuzeDownloadManager dm =
        VuzeDownloadFactory.create(
            torrentFile.getAbsolutePath(),
            null,
            saveDir.getAbsolutePath(),
            new VuzeDownloadListener() {

              @Override
              public void stateChanged(VuzeDownloadManager dm, int state) {
                if (state == VuzeDownloadManager.STATE_STOPPED) {
                  signal.countDown();
                }
              }

              @Override
              public void downloadComplete(VuzeDownloadManager dm) {}
            });

    assertNotNull(dm);

    VuzeUtils.remove(dm, true);

    assertTrue("Unable to remove the download", TestUtils.await(signal, 10, TimeUnit.SECONDS));
  }
  public SoundcloudDownload(SoundcloudSearchResult sr) {
    this.sr = sr;
    this.size = sr.getSize();

    String filename = sr.getFilename();

    completeFile = buildFile(SharingSettings.TORRENT_DATA_DIR_SETTING.getValue(), filename);
    tempAudio = buildTempFile(FilenameUtils.getBaseName(filename), "mp3");

    bytesReceived = 0;
    dateCreated = new Date();

    httpClientListener = new HttpDownloadListenerImpl();

    httpClient = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.DOWNLOAD);
    httpClient.setListener(httpClientListener);

    start();
  }
  private List<LinkInfo> extractLinksFromDashManifest(
      String dashManifestUrl,
      YouTubeSig ytSig,
      String filename,
      Date date,
      String videoId,
      String userName,
      String channelName,
      ThumbnailLinks thumbnailLinks)
      throws IOException, ParserConfigurationException, SAXException {
    dashManifestUrl = dashManifestUrl.replace("\\/", "/");
    Pattern p = Pattern.compile("/s/([a-fA-F0-9\\.]+)/");
    Matcher m = p.matcher(dashManifestUrl);
    if (m.find()) {
      String sig = m.group(1);
      String signature = ytSig.calc(sig);

      dashManifestUrl =
          dashManifestUrl.replaceAll("/s/([a-fA-F0-9\\.]+)/", "/signature/" + signature + "/");
    } else if (dashManifestUrl.contains("/signature/")) {
      // dashManifestUrl as it is, empty block to review
    } else {
      return Collections.emptyList();
    }

    HttpClient httpClient = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.SEARCH);
    String dashDoc = httpClient.get(dashManifestUrl);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(dashDoc)));

    NodeList nodes = doc.getElementsByTagName("BaseURL");

    List<LinkInfo> infos = new ArrayList<LinkInfo>();

    for (int i = 0; i < nodes.getLength(); i++) {
      Node item = nodes.item(i);
      String url = item.getTextContent();
      int contentLength = -1;
      try {
        contentLength = Integer.parseInt(item.getAttributes().item(0).getTextContent());
      } catch (Throwable e) {
        // ignore
      }
      int fmt = Integer.parseInt(new Regex(url, "itag=(\\d+)").getMatch(0));

      Format format = FORMATS.get(fmt);
      if (format == null) {
        continue;
      }

      LinkInfo info =
          new LinkInfo(
              url,
              fmt,
              filename,
              contentLength,
              date,
              videoId,
              userName,
              channelName,
              thumbnailLinks,
              format);
      infos.add(info);
    }

    return infos;
  }