Java download speed measurement

I am working on uploading a file to the software, this is what I got, it is downloaded, and I can also make progress, but still there is one thing that I don’t know how to do. Measure the download speed. I would be grateful for your help. Thank you This is the current boot method code.

public void run() { OutputStream out = null; URLConnection conn = null; InputStream in = null; try { URL url1 = new URL(url); out = new BufferedOutputStream( new FileOutputStream(sysDir+"\\"+where)); conn = url1.openConnection(); in = conn.getInputStream(); byte[] buffer = new byte[1024]; int numRead; long numWritten = 0; double progress1; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); numWritten += numRead; this.speed= (int) (((double) buffer.length)/8); progress1 = (double) numWritten; this.progress=(int) progress1; } } catch (Exception ex) { echo("Unknown Error: " + ex); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException ex) { echo("Unknown Error: " + ex); } } } 
+6
source share
3 answers

In the same way, you can measure something.

System.nanoTime() returns a Long , which you can use to determine how long it takes:

 Long start = System.nanoTime(); // do your read Long end = System.nanoTime(); 

You now have the number of nanoseconds required to read X bytes. Do the math and you have the download speed.

You are most likely looking for bytes per second. Keep track of the total number of bytes read, checking to see if one second has passed. Once one second has gone, determine the speed based on how many bytes you read in that amount of time. Reset total, repeat.

+8
source

I can give you a general idea. Start the timer at the start of the download. Now multiply (percentage downloaded) by download size and divide it by time elapsed. . This will give you an average load time. I hope I get you back on the right track!

You can use System.nanoTime(); as suggested by Brian.

Put long startTime = System.nanoTime(); outside the while loop. and

long estimatedTime = System.nanoTime() - startTime; will give you elapsed time in your cycle.

+1
source

here is my implementation

 while (mStatus == DownloadStatus.DOWNLOADING) { /* * Size buffer according to how much of the file is left to * download. */ byte buffer[]; // handled resume case. if ((mSize < mDownloaded ? mSize : mSize - mDownloaded <= 0 ? mSize : mSize - mDownloaded) > MAX_BUFFER_SIZE) { buffer = new byte[MAX_BUFFER_SIZE]; } else { buffer = new byte[(int) (mSize - mDownloaded)]; } // Read from server into buffer. int read = stream.read(buffer); if (read == -1) break;// EOF, break while loop // Write buffer to file. file.write(buffer, 0, read); mDownloaded += read; double speedInKBps = 0.0D; try { long timeInSecs = (System.currentTimeMillis() - startTime) / 1000; //converting millis to seconds as 1000m in 1 second speedInKBps = (mDownloaded / timeInSecs) / 1024D; } catch (ArithmeticException ae) { } this.mListener.publishProgress(this.getProgress(), this.getTotalSize(), speedInKBps); } 
+1
source

Source: https://habr.com/ru/post/890343/


All Articles