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.
 
    