content: /*
   * Copyright (c) 2020-2025, Alden Torres
   *
   * Licensed under the terms of the MIT license.
   * Copy of the license at https://opensource.org/licenses/MIT
   */
  
  package org.libtorrent4j.demo;
  
  import org.libtorrent4j.AlertListener;
  import org.libtorrent4j.LibTorrent;
  import org.libtorrent4j.SessionManager;
  import org.libtorrent4j.TorrentInfo;
  import org.libtorrent4j.alerts.AddTorrentAlert;
  import org.libtorrent4j.alerts.Alert;
  import org.libtorrent4j.alerts.AlertType;
  import org.libtorrent4j.alerts.BlockFinishedAlert;
  
  import java.io.File;
  import java.util.concurrent.CountDownLatch;
  
  /**
   * @author gubatron
   * @author aldenml
   */
  public final class DownloadTorrent {
  
      public static void main(String[] args) throws Throwable {
  
          File torrentFile = new File(args[0]);
  
          System.out.println("Using libtorrent version: " + LibTorrent.version());
  
          final SessionManager s = new SessionManager();
  
          final CountDownLatch signal = new CountDownLatch(1);
  
          s.addListener(new AlertListener() {
              @Override
              public int[] types() {
                  return null;
              }
  
              @Override
              public void alert(Alert<?> alert) {
                  AlertType type = alert.type();
  
                  switch (type) {
                      case ADD_TORRENT:
                          System.out.println("Torrent added");
                          ((AddTorrentAlert) alert).handle().resume();
                          break;
                      case BLOCK_FINISHED:
                          BlockFinishedAlert a = (BlockFinishedAlert) alert;
                          int p = (int) (a.handle().status().progress() * 100);
                          System.out.println("Progress: " + p + " for torrent name: " + a.handle().getName());
                          System.out.println(s.stats().totalDownload());
                          break;
                      case TORRENT_FINISHED:
                          System.out.println("Torrent finished");
                          signal.countDown();
                          break;
                  }
              }
          });
  
          s.start();
  
          TorrentInfo ti = new TorrentInfo(torrentFile);
          s.download(ti, torrentFile.getParentFile());
  
          signal.await();
  
          s.stop();
      }
  }
  