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);
}
}
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);
}
}
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();
}
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();
}
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("没有选择下载文件");
}
}
Aggregations