0

yI use the following code to download a file from internet.

//suppose I call this method inside main(String[] st) method.
private void download(String link){
  URL url = new URL(link);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestProperty("Accept-Encoding", "identity");
  connection.setRequestProperty("connection", "close");
  connection.connect();
  save(connection.getInputStream());
}

private final int DOWNLOAD_BUFFER_SIZE = 10 * 1024;
  private void save(InputStream inputStream) {
    FileOutputStream fos;
    try {
      byte[] buffer = new byte[DOWNLOAD_BUFFER_SIZE];
      int len;
      fos = new FileOutputStream("FILE_PATH");
      while ((len = inputStream.read(buffer)) != -1) {
        fos.write(buffer, 0, len);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      //close streams, flush, etc.
    }
  }

Everything is OK and the code works fine. But when I change the value of DOWNLOAD_BUFFER_SIZE to small numbers like 5, an strange thing happens! When I change the value of DOWNLOAD_BUFFER_SIZE to small numbers, and I turn off my internet connection, download continues for a while and does not stop! But when the value of DOWNLOAD_BUFFER_SIZE is big like 10 * 1024, everything is OK and when I turn off my connection download stops.

Can anyone help me? I want my download to be stopped when the value of DOWNLOAD_BUFFER_SIZE is small and I turn off my internet connection.

I have seen this link but didn't help.

the EJP's answer does not contain the solution to solve this problem. i want DOWNLOAD_BUFFER_SIZE to be small, because of some reasons.

Please help me. I need your help. please:X

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

Hadi
  • 544
  • 1
  • 8
  • 28

1 Answers1

2

Because of the socket receive buffer. There is only so much in it. If you read small chunks out of it, it takes more reads.

I don't know why you want the size of your download buffer to be small. I usually use 4096 or 8192 bytes. Setting it to such a large value as 1MB is pointless unless the socket receive buffer in the kernel is equally large.

If you're concerned about network outages, as you should be, you should set a read timeout on the socket.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I set readTimeout to 250, 500 , etc. but the result does not change!! the post edited. if you can, please help me. – Hadi May 01 '18 at 19:26
  • The result being what? The result *should* be that you read all the data in the buffer and then get a `SocketTimeoutException`. – user207421 May 01 '18 at 22:37
  • sorry, i can not speak english well. i do not wamt to set Read timeout on the socket. i just want my download to be stopped when Downlpad_Buffer_size is small and i turn off my internet. Is ther any way to do this? and is this possible? – Hadi May 02 '18 at 05:32
  • I do not understand. Why you do want to use a small buffer? and why don't you want to use a read timeout? In both cases you are simply making wrong choices. – user207421 May 02 '18 at 07:12
  • OK. Thanks. i'll use a big buffer. Thanks again. – Hadi May 02 '18 at 08:24
  • please look at my new question about the same problem. https://stackoverflow.com/questions/50135938/downloading-does-not-stop-when-internet-connection-lost – Hadi May 02 '18 at 13:32