There is a method for downloading a file via Http:

private static void downloadFile(String strURL, String strPath, int buffSize) { try { /* Get connection */ URL connection = new URL(strURL); HttpURLConnection urlconn; urlconn = (HttpURLConnection) connection.openConnection(); urlconn.setRequestMethod("GET"); urlconn.connect(); /* Set input stream */ InputStream in = null; in = urlconn.getInputStream(); /* Set write stream */ OutputStream writer = new FileOutputStream(strPath); byte buffer[] = new byte[buffSize]; // Max bytes per one reception /* Download */ int i = 0; while ((i = in.read(buffer)) > 0) { writer.write(buffer, 0, i); } /* Cleaning */ writer.flush(); writer.close(); in.close(); } catch (IOException e) { System.out.println(e); } } 

I like the simple output like: System.out.println("Speed: " + Mb + "/sec");

I wanted to use the Timer and TimerTask . But, their methods schedule() , scheduleAtFixedRate() not suitable, since they are executed only after Thread.sleep(x); but I don't know how long ( x ) the download will take.

I can output the number of bytes i received in one iteration of the loop (~ 1ms), but I want it in 1s. It is possible, of course, to add a variable number of ms to int , to another number of bytes, and when there will be> 1000 ms output speed, but surely there is a better way?

  • You can use a moving average. findicators.com/formula-moving-average Convenient display of the average speed at each time. - DimXenon
  • I would suggest updating the speed after receiving the data. To do this, save the time (long) to start downloading. And create a variable - how many bytes priyanto. Well, at the time of reception, change the value of the byte and recalculate the speed. - pavel
  • @DimXenon is possible, but the point is how to use it - Lurking Elk
  • Set the time variable to the value of System.currentTimeMillis() before the loop. Then at the end of the iteration we call the method again and see the difference. Equal to or greater than a second - send the speed value to the method of calculating the value for the moving average. - DimXenon
  • And it is possible and simply in each iteration to send data to the SS calculation method. And it is not necessary to do precisely and unnecessary actions. - DimXenon

1 answer 1

In general, in the absence of the best, I offer my crutch, sung and described in a post-question:

 private static void downloadFile(String strURL, String strPath, int buffSize) { try { /* Get connection */ URL connection = new URL(strURL); HttpURLConnection urlconn; urlconn = (HttpURLConnection) connection.openConnection(); urlconn.setRequestMethod("GET"); urlconn.connect(); /* Set input stream */ InputStream in = null; in = urlconn.getInputStream(); /* Set write stream */ OutputStream writer = new FileOutputStream(strPath); byte buffer[] = new byte[buffSize]; // Max bytes per one reception /* Download */ int i = 0; double getted_b = 0.0; long delta_t = System.nanoTime(); while ((i = in.read(buffer)) > 0) { getted_b += i; writer.write(buffer, 0, i); if ((System.nanoTime() - delta_t) >= 1E9) { // If the second was held int kb, mb; mb = new Double(getted_b / (1024 * 1024)).intValue(); kb = new Double((getted_b / 1024) % 1024).intValue(); System.out.println(" >> Speed: " + mb + " " + kb + " Mb/sec"); delta_t = System.nanoTime(); // Set to zero getted_b = 0.0; } } /* Cleaning */ writer.flush(); writer.close(); in.close(); } catch (IOException e) { System.out.println(e); } }