lingochamp / FileDownloader

Multitask、MultiThread(MultiConnection)、Breakpoint-resume、High-concurrency、Simple to use、Single/NotSingle-process
Apache License 2.0
11.02k stars 2.2k forks source link

自定义FileDownloadOutputStream失败 #1045

Open Chihiro23333 opened 6 years ago

Chihiro23333 commented 6 years ago

不知为何自定义FileDownloadOutputStream后

   FileDownloader.setupOnApplicationOnCreate(this)
                .maxNetworkThreadCount(5)
                .connectionCreator(new FileDownloadUrlConnection
                        .Creator(new FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15_000) // set connection timeout.
                        .readTimeout(15_000) // set read timeout.
                        .proxy(Proxy.NO_PROXY) // set proxy
                ))
                .outputStreamCreator(new FileDownloadHelper.OutputStreamCreator() {
                    @Override
                    public FileDownloadOutputStream create(File file) throws IOException {
                        return new CustomFileDownloadRandomAccessFile.Creator().create(file);
                    }

                    @Override
                    public boolean supportSeek() {
                        return true;
                    }
                })
                .commit();

使用时,不会走我自定义的FileDownloadOutputStream,还是会走默认的FileDownloadRandomAccessFile 研读了一下代码无果,希望能帮忙解决,非常感谢

rantianhua commented 6 years ago

你的这行代码看着很奇怪:

@Override
public FileDownloadOutputStream create(File file) throws IOException {
    return new CustomFileDownloadRandomAccessFile.Creator().create(file);
}

你的实现应该参照 FileDownloadRandomAccessFile.Creator 就好了:

public static class Creator implements FileDownloadHelper.OutputStreamCreator {

        @Override
        public FileDownloadOutputStream create(File file) throws IOException {
            return new FileDownloadRandomAccessFile(file);
        }

        @Override
        public boolean supportSeek() {
            return true;
        }
    }

请帖一下你 CustomFileDownloadRandomAccessFile 的实现。

Chihiro23333 commented 6 years ago

现在的需求是我准备实现简单的文件加密,然后我自定义了一个输出流实现了FileDownloadOutputStream接口,也就是上面的CustomFileDownloadRandomAccessFile ,然后在write方法里面做了一些加密操作,但是现在的问题是我虽然自定义了FileDownloadOutputStream,但是运行时默认还是会走到默认的FileDownloadRandomAccessFile,CustomFileDownloadRandomAccessFile的实现和默认的FileDownloadRandomAccessFile一样,只是write方法有稍许不同,我在你的demo中也会有这个问题