All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.schoolwow.download.module.task.flow.downloader.SingleThreadDownloadFlow Maven / Gradle / Ivy

The newest version!
package cn.schoolwow.download.module.task.flow.downloader;

import cn.schoolwow.download.domain.pool.PoolConfig;
import cn.schoolwow.download.domain.progress.DownloadProgress;
import cn.schoolwow.download.domain.progress.GetFileDownloadedSize;
import cn.schoolwow.download.domain.task.DownloadTask;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.quickhttp.response.Response;

import java.io.File;

public class SingleThreadDownloadFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        PoolConfig poolConfig = flowContext.checkInstanceData(PoolConfig.class);
        DownloadTask downloadTask = flowContext.checkInstanceData(DownloadTask.class);
        DownloadProgress downloadProgress = flowContext.checkInstanceData(DownloadProgress.class);
        Response response = (Response) flowContext.checkData("response");
        File file = (File) flowContext.checkData("file");

        downloadProgress.getFileDownloadedSize = new GetFileDownloadedSize() {
            @Override
            public long getFileDownloadedSize() {
                return file.length();
            }
        };
        int maxDownloadSpeed = downloadTask.maxDownloadSpeed>0?downloadTask.maxDownloadSpeed:poolConfig.maxDownloadSpeed;
        response.maxDownloadSpeed(maxDownloadSpeed).bodyAsFile(file.toPath());
        response.close();
    }

    @Override
    public String name() {
        return "单线程下载流程";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy