Measuring Download Speed Java

2019-01-18 06:48发布

I'm working on downloading a file on a software, this is what i got, it sucesfully download, and also i can get progress, but still 1 thing left that I dont know how to do. Measure download speed. I would appreciate your help. Thanks. This is the current download 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);
            }
        }
    }

3条回答
\"骚年 ilove
2楼-- · 2019-01-18 07:07

The same way you would measure anything.

System.nanoTime() returns a Long you can use to measure how long something takes:

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

Now you have the number of nanoseconds it took to read X bytes. Do the math and you have your download rate.

More than likely you're looking for bytes per second. Keep track of the total number of bytes you've read, checking to see if one second has elapsed. Once one second has gone by figure out the rate based on how many bytes you've read in that amount of time. Reset the total, repeat.

查看更多
Animai°情兽
3楼-- · 2019-01-18 07:21

I can give you a general idea. Start a timer at the beginning of the download. Now, multiply the (percentage downloaded) by the download size, and divide it by the time elapsed. That gives you average download time. Hope I get you on the right track!

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

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

long estimatedTime = System.nanoTime() - startTime; will give you the elapsed time within your loop.

查看更多
劫难
4楼-- · 2019-01-18 07:24

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);
        }
查看更多
登录 后发表回答