use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class TorrentContext method loadTorrent.
/**
* <p>种子文件加载</p>
*
* @param path 种子文件地址
*
* @return 种子信息
*
* @throws DownloadException 下载异常
*/
public static final Torrent loadTorrent(String path) throws DownloadException {
final File file = new File(path);
if (!file.exists()) {
throw new DownloadException("不存在的种子文件");
}
try {
final var bytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
final var decoder = BEncodeDecoder.newInstance(bytes).next();
if (decoder.isEmpty()) {
throw new DownloadException("种子文件格式错误");
}
final var torrent = Torrent.valueOf(decoder);
// 直接转储原始信息:防止顺序不对导致种子Hash计算错误
final var info = decoder.getMap(Torrent.ATTR_INFO);
final var infoHash = InfoHash.newInstance(BEncodeEncoder.encodeMap(info));
torrent.infoHash(infoHash);
return torrent;
} catch (NetException | IOException e) {
throw new DownloadException("种子文件加载失败", e);
}
}
use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class SingleFileDownloader method buildOutput.
/**
* <p>新建{@linkplain #output 输出流}</p>
* <p>通过判断任务已经下载大小验证是否支持断点续传</p>
*
* @throws DownloadException 下载异常
*/
protected void buildOutput() throws DownloadException {
try {
final long size = this.taskSession.downloadSize();
final int bufferSize = DownloadConfig.getMemoryBufferByte(this.taskSession.getSize());
OutputStream outputStream;
if (size > 0L) {
// 支持断点续传
outputStream = new FileOutputStream(this.taskSession.getFile(), true);
} else {
// 不支持断点续传
outputStream = new FileOutputStream(this.taskSession.getFile());
}
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream, bufferSize);
this.output = Channels.newChannel(bufferedOutputStream);
} catch (FileNotFoundException e) {
throw new DownloadException("下载文件打开失败", e);
}
}
use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class TsLinkerTest method testLink.
@Test
void testLink() throws DownloadException {
final String name = "测试";
final String filePath = "E:\\snail\\m3u8";
final File parent = new File(filePath);
final var links = List.of(parent.listFiles()).stream().map(file -> file.getAbsolutePath()).filter(// 排除生成文件:防止重复读写
path -> !path.contains(name)).collect(Collectors.toList());
Cipher cipher = null;
// final var builder = M3u8Builder.newInstance(new File("E://snail/index.m3u8"), "https://www.acgist.com/a/b?v=1234");
// cipher = builder.build().getCipher();
final TsLinker linker = TsLinker.newInstance(name, filePath, cipher, links);
linker.link();
File file = Paths.get(filePath, "测试.ts").toFile();
assertTrue(file.exists());
FileUtils.delete(file);
}
use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class Protocol method buildTaskSession.
/**
* <p>新建下载任务</p>
*
* @param url 下载链接
*
* @return 任务信息
*
* @throws DownloadException 下载异常
*/
public synchronized ITaskSession buildTaskSession(String url) throws DownloadException {
this.url = url.strip();
boolean success = true;
try {
this.buildTaskEntity();
return TaskSession.newInstance(this.taskEntity);
} catch (DownloadException e) {
success = false;
throw e;
} catch (Exception e) {
success = false;
throw new DownloadException("下载失败", e);
} finally {
if (success) {
this.success();
}
this.release(success);
}
}
Aggregations