Results 1 to 3 of 3
  1. #1
    MGT Madness is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Current Download Speed

    Is there a way to measure current download speed of a file being downloaded?
    I've searched all over google and the only thing i get is average download speed :/]

    This is basically how it is being done, the result is average download speed.
    Java Code:
    OutputStream out = null;
    URLConnection conn;
    InputStream in = null;
    
    long startup = System.currentTimeMillis();
    
    try {
            URL url = new URL(adress);
            out = new BufferedOutputStream(
                    new FileOutputStream(getCacheDir() + "/" + localFileName));
    
            conn = url.openConnection(); in = conn.getInputStream();
    
            byte[] data = new byte[BUFFER];
    
            int numRead;
    
    
            while ((numRead = in .read(data)) != -1) {
                    out.write(data, 0, numRead);
                    long max = conn.getContentLength();
                    long curr = 0;
                    byte[] b = new byte[1024];
                    int len;
                    while ((len = in .read(b, 0, b.length)) > -1) {
                            out.write(b, 0, len);
                            curr += len;
    
                            try {
                                    long time = (System.currentTimeMillis() - startup) / 1000;
                                    kbps = (curr / time) / 1024D;
                            } catch (ArithmeticException e) {
    
                            }
    
                            setDownloadPercent((int)((curr * 100) / max));
                    }
            }
    
    } catch (Exception exception) {
            exception.printStackTrace();
    } finally {
            try {
                    if ( in != null) { in .close();
                    }
                    if (out != null) {
                            out.close();
                    }
            } catch (IOException ioe) {}
    }
    - Thank you.
    Last edited by MGT Madness; 01-05-2014 at 10:20 PM.

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Current Download Speed

    Every time a second passes just check the amount of kbs, mbs, or gbs that were downloaded in that second.

  3. #3
    MGT Madness is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Current Download Speed

    Great idea thanks, i'll try this out.

Similar Threads

  1. Speed up program?
    By Zigster in forum New To Java
    Replies: 5
    Last Post: 04-25-2012, 02:51 PM
  2. Set max upload speed
    By Giuseppe_Mazzei in forum New To Java
    Replies: 9
    Last Post: 06-03-2011, 02:28 AM
  3. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •