Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring Download Speed Java

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);
            }
        }
    }
like image 677
Jesus David Gulfo Agudelo Avatar asked Jun 12 '11 15:06

Jesus David Gulfo Agudelo


People also ask

How to calculate download speed in Java?

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.

What is download speed?

What Is Download Speed? Your download speed refers to how quickly you receive text, images, music, video, and other data online. Most online activity involves downloading, including viewing web pages, streaming video, and gaming. Your ISP and data plan partially determine your download speed.

Whats upload speed?

Upload speed is the speed your Internet connection requires to send data from your device to another device or to the Internet. Upload speeds are pertinent for video chats, downloading large files, or live streaming video.


2 Answers

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.

like image 53
Brian Roach Avatar answered Sep 18 '22 22:09

Brian Roach


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);
        }
like image 42
AZ_ Avatar answered Sep 21 '22 22:09

AZ_