Hadi
Hadi

Reputation: 574

Why download continues without any connection?

I've asked a similar question in this link before but i still have this problem. This a code to explain my problem:

package main;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainClass {

    public static void main(String[] args) throws IOException {
        URL url = new URL("http://speedtest-ny.turnkeyinternet.net/100mb.bin");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setReadTimeout(10000);
        con.setConnectTimeout(10000);
        con.connect();
        InputStream inputStream = con.getInputStream();
        int len;
        byte[] buffer = new byte[1024];
        while ((len = inputStream.read(buffer)) != -1) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("downlaoding : " + len);
        }
        System.out.println("finished");
    }

}

if I disconnect my laptop from internet during download, download does not end and continues for a while(depending on the time i disconnect my laptop from internet). My questions are:

  1. why does this happen?
  2. how can I solve this problem?

Please help me. I really need your answer.

I apologize in advance if the grammar of my sentences is not correct. Because I can't speak English well.

Upvotes: 0

Views: 41

Answers (1)

Joe C
Joe C

Reputation: 15694

You appear to be under the impression that this code limits the download speed to 1 kB/s. That is not the case.

This data will be downloaded to your machine as quickly as the server and your ISP are able to send it (subject to the size of your machine's network buffer), and it will be buffered somewhere in your OS until the Java process requests it.

Upvotes: 1

Related Questions