0

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.

Mike Kinghan
  • 46,463
  • 8
  • 124
  • 156
Hadi
  • 642
  • 7
  • 21
  • 1
    Given how slowly you are processing the data (1 kB/s), I suspect the data is already downloaded and is just buffered waiting for your process to handle it. You can use a network monitoring tool (such as Wireshark) to confirm this. – Joe C Oct 28 '18 at 11:58
  • @JoeC Do you mean this code does not limit download speed to 1 Kb/s ? – Hadi Oct 28 '18 at 12:32
  • That is correct. – Joe C Oct 28 '18 at 12:40

1 Answers1

1

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.

Joe C
  • 13,953
  • 7
  • 35
  • 48