【问题标题】:Interrupt download which uses dataInputStream使用 dataInputStream 的中断下载
【发布时间】:2019-09-25 13:26:14
【问题描述】:

有一个非常简单的下载文件代码。

class Downloader {
private static Downloader Dinstance = null;
private boolean isAborted = false;

void setAborted(boolean isAborted) {
    isAborted = true;
}

int getAborted() {
    return isAborted;
}

static Downloader getInstance(@NonNull Context context) {
        if (Dinstance == null)
            Dinstance = new Downloaderr(context.getApplicationContext());
        return Dinstance;
    }

private Downloader() {
    ...
    }

function download() {
    ...
    try {
            URL url = new URL(RequestedURL);
            InputStream inputStream = url.openStream();
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            FileOutputStream fileOutputStream = new FileOutputStream(FileName);
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = dataInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, bytesRead);
                if (getAborted()) {
                    Log.d(static_variables.TAG, "Aborting 1");
                    fileOutputStream.close();
                    Log.d(static_variables.TAG, "Aborting 2");
                    dataInputStream.close();
                    Log.d(static_variables.TAG, "Aborting 3");
                    inputStream.close();
                    Log.d(static_variables.TAG, "All closed");                        
                    file.delete();
                    downloadFinished = true;
                    Log.d(static_variables.TAG, "Returning");
                    return false;
                    }
            fileOutputStream.close();
            dataInputStream.close();
            inputStream.close();
        } catch (IOException e) {
            downloadFinished = true;
            return false;
        }
    ...
    }
}

在主活动中

第一个按钮监听器(开始下载):

function onClick() {
   new Thread(new Runnable() {
       @Override
       public void run() {
           Downloader.getInstance().download(...);
           }
       }).start();
   }

第二个按钮监听器:

function onClick() {
   Downloader.getInstance().setAborted(true);
   }

在线程中下载。 在日志中dataInputStream.close(); 的时间最长。其他研究表明,在文件完全下载之前,流不会关闭。

如何终止正在进行的InputStream

【问题讨论】:

  • 请显示您的完整代码以及停止/中止下载的位置。
  • 我重写了代码。这不是我的完整代码。但是结构是一样的。我实际上不明白为什么需要它。因为出现logcat 中的日志消息。正如我认为结构正在运行,InputStream 终止的问题

标签: android download inputstream datainputstream


【解决方案1】:

在研究过程中,我发现了两件事。

  1. 使用这种方法下载会中止,但是有一个缓冲区,对于小文件(在我的情况下是歌曲)几乎无法跟踪。对于 500Mb 文件下载,当你在 15Mb 下载时按 abort,然后在 16-17 之后它将停止。

  2. 如果您想(像我一样)更快地终止下载,那么您应该使用另一种方法。启动 Thread 时,保存指针 int 变量。

Thread threadDownload = new Thread(...);
threadDownload.start();

然后使用中止

threadDownload.interrupt();

小心。您需要自己删除临时文件。您在OutputStream 中使用的文件。

您还会有一些缓冲区开销,但不是 1-2Mb,而是 200-300Kb。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多