Search in sources :

Example 1 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class SingleFileDownloader method download.

@Override
public void download() throws DownloadException {
    int length = 0;
    final long fileSize = this.taskSession.getSize();
    final ByteBuffer buffer = ByteBuffer.allocateDirect(SystemConfig.DEFAULT_EXCHANGE_LENGTH);
    try {
        while (this.downloadable()) {
            length = this.input.read(buffer);
            if (length >= 0) {
                buffer.flip();
                this.output.write(buffer);
                buffer.compact();
                this.statistics.download(length);
                this.statistics.downloadLimit(length);
                this.fastCheckTime = System.currentTimeMillis();
            }
            if (Downloader.checkFinish(length, this.taskSession.downloadSize(), fileSize)) {
                this.completed = true;
                break;
            }
        }
    } catch (IOException e) {
        // 防止偶然下载失败:通过验证下载时间、下载数据大小进行重试下载
        throw new DownloadException("数据流操作失败", e);
    }
}
Also used : DownloadException(com.acgist.snail.context.exception.DownloadException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class TorrentController method buildTree.

/**
 * <p>新建文件选择树形菜单</p>
 *
 * @param taskSession 任务信息
 */
public void buildTree(ITaskSession taskSession) {
    Torrent torrent = null;
    final TreeView<HBox> tree = this.buildTree();
    try {
        torrent = TorrentContext.getInstance().newTorrentSession(taskSession.getTorrent()).torrent();
    } catch (DownloadException e) {
        LOGGER.error("种子文件解析异常", e);
        Alerts.warn("下载失败", e.getMessage());
    }
    if (torrent != null) {
        this.taskSession = taskSession;
        this.torrentSelector = TorrentSelector.newInstance(torrent.name(), this.download, tree);
        torrent.getInfo().files().stream().filter(TorrentFile::notPaddingFile).forEach(this.torrentSelector::build);
        this.torrentSelector.select(taskSession);
    }
}
Also used : Torrent(com.acgist.snail.pojo.bean.Torrent) HBox(javafx.scene.layout.HBox) DownloadException(com.acgist.snail.context.exception.DownloadException)

Example 3 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class TaskContextTest method testNewTask.

@Test
void testNewTask() throws DownloadException {
    SnailBuilder.newBuilder().buildSync();
    final var taskContext = TaskContext.getInstance();
    var exception = assertThrows(DownloadException.class, () -> taskContext.download("https://www.acgist.com"));
    this.log(exception.getMessage());
    exception = assertThrows(DownloadException.class, () -> taskContext.submit(null));
    this.log(exception.getMessage());
    ProtocolContext.getInstance().register(HttpProtocol.getInstance());
    final ITaskSession taskSession = taskContext.download("https://www.acgist.com");
    assertTrue(taskContext.allTask().size() > 0);
    FileUtils.delete(taskSession.getFile());
    taskSession.delete();
}
Also used : ITaskSession(com.acgist.snail.pojo.ITaskSession) DownloadException(com.acgist.snail.context.exception.DownloadException) Test(org.junit.jupiter.api.Test)

Example 4 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class SnailTest method testTorrent.

@Test
void testTorrent() throws DownloadException {
    final String torrentPath = "E:\\snail\\0B156834B59B0FF64EE0C9305D4D6EDE421196E6.torrent";
    final var snail = SnailBuilder.newBuilder().enableTorrent().buildSync();
    assertNotNull(snail);
    // 解析种子文件
    final var torrent = TorrentContext.loadTorrent(torrentPath);
    // 过滤下载文件
    final var list = torrent.getInfo().files().stream().filter(TorrentFile::notPaddingFile).map(TorrentFile::path).filter(path -> path.endsWith(".mkv")).collect(Collectors.toList());
    // 设置下载文件
    GuiContext.getInstance().files(DescriptionWrapper.newEncoder(list).serialize());
    // 注册文件选择事件
    GuiContext.register(new MultifileEventAdapter());
    // 开始下载
    snail.download(torrentPath);
    snail.lockDownload();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Socket(java.net.Socket) Performance(com.acgist.snail.utils.Performance) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) GuiContext(com.acgist.snail.context.GuiContext) DownloadException(com.acgist.snail.context.exception.DownloadException) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) Test(org.junit.jupiter.api.Test) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SystemConfig(com.acgist.snail.config.SystemConfig) DescriptionWrapper(com.acgist.snail.pojo.wrapper.DescriptionWrapper) SnailBuilder(com.acgist.snail.Snail.SnailBuilder) MultifileEventAdapter(com.acgist.snail.gui.event.adapter.MultifileEventAdapter) TorrentContext(com.acgist.snail.context.TorrentContext) MultifileEventAdapter(com.acgist.snail.gui.event.adapter.MultifileEventAdapter) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) Test(org.junit.jupiter.api.Test)

Example 5 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class TorrentProtocol method selectFiles.

/**
 * <p>选择下载文件、设置文件大小</p>
 *
 * @throws DownloadException 下载异常
 */
private void selectFiles() throws DownloadException {
    ITaskSession taskSession = null;
    try {
        taskSession = TaskSession.newInstance(this.taskEntity);
        GuiContext.getInstance().multifile(taskSession);
    } catch (DownloadException e) {
        throw e;
    } catch (Exception e) {
        throw new DownloadException("选择下载文件错误", e);
    }
    if (taskSession.multifileSelected().isEmpty()) {
        throw new DownloadException("没有选择下载文件");
    }
}
Also used : ITaskSession(com.acgist.snail.pojo.ITaskSession) DownloadException(com.acgist.snail.context.exception.DownloadException) DownloadException(com.acgist.snail.context.exception.DownloadException)

Aggregations

DownloadException (com.acgist.snail.context.exception.DownloadException)14 ITaskSession (com.acgist.snail.pojo.ITaskSession)3 File (java.io.File)3 IOException (java.io.IOException)3 Test (org.junit.jupiter.api.Test)3 NetException (com.acgist.snail.context.exception.NetException)2 Performance (com.acgist.snail.utils.Performance)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 IContext (com.acgist.snail.IContext)1 Snail (com.acgist.snail.Snail)1 SnailBuilder (com.acgist.snail.Snail.SnailBuilder)1 SystemConfig (com.acgist.snail.config.SystemConfig)1 GuiContext (com.acgist.snail.context.GuiContext)1 TorrentContext (com.acgist.snail.context.TorrentContext)1 IDownloader (com.acgist.snail.downloader.IDownloader)1 MultifileEventAdapter (com.acgist.snail.gui.event.adapter.MultifileEventAdapter)1 Logger (com.acgist.snail.logger.Logger)1 LoggerFactory (com.acgist.snail.logger.LoggerFactory)1