By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,800 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,800 IT Pros & Developers. It's quick & easy.

How to increase or decrease the speed of Download process in Java?

P: 28
Here is my DownloadFile.java.
Expand|Select|Wrap|Line Numbers
  1. public class DownloadFile extends Download implements Runnable, DownloadFileInterface {
  2.  
  3.     private String name; //get name of the file
  4.  
  5.     public DownloadFile(URL url, int id) {
  6.         super(url, id);
  7.  
  8.     }
  9.  
  10.     // Start downloading.
  11.     public void download() {
  12.         System.out.println("Downloading...");
  13.         InputStream stream = null;
  14.         File file = new File(getUrl());
  15.         RandomAccessFile rfile = null;
  16.         System.out.println("Download started: " + getId());
  17.         setName(file.getFileName());
  18.         try {
  19.  
  20.             // Open connection to URL.
  21.             HttpURLConnection connection = (HttpURLConnection) getUrl()
  22.                     .openConnection();
  23.  
  24.             // Specify what portion of file to download.
  25.             connection.setRequestProperty("Range", "bytes=" + getDownloaded() + "-");
  26.  
  27.             // Connect to server.
  28.             connection.connect();
  29.  
  30.             int contentLength = connection.getContentLength();
  31.             System.out.println(contentLength);
  32.  
  33.             /*
  34.              * Set the size for this download if it hasn't been already set.
  35.              */
  36.             if (getSize() == -1) {
  37.                 setSize(contentLength);
  38.  
  39.             }
  40.  
  41.             // Open file and seek to the end of it.
  42.             rfile = file.openRandomAccessFile();
  43.             rfile.seek(getDownloaded());
  44.  
  45.             stream = connection.getInputStream();
  46.  
  47.             while (getStatus() == DOWNLOADING) {
  48.                 /*
  49.                  * Size buffer according to how much of the file is left to
  50.                  * download.
  51.                  */
  52.                 byte buffer[];
  53.  
  54.                 if (getSize() - getDownloaded() > MAX_BUFFER_SIZE) {
  55.                     buffer = new byte[MAX_BUFFER_SIZE];
  56.                     // System.out.println("id: " + id + " downloaded byte: " +
  57.                     // MAX_BUFFER_SIZE);
  58.                 } else {
  59.                     int remainingByte = getSize() - getDownloaded();
  60.                     if (remainingByte <= 0) {
  61.  
  62.                         System.out.println(file.getFileName()
  63.                                 + " has been downloaded");
  64.                         break;
  65.                     }
  66.  
  67.                     else {
  68.                         buffer = new byte[remainingByte];
  69.                         // System.out.println("id: " + id + " remaining byte: "
  70.                         // + remainingByte);
  71.                     }
  72.                 }
  73.  
  74.                 // Read from server into buffer.
  75.                 int read = stream.read(buffer);
  76.  
  77.                 // Write buffer to file.
  78.                 rfile.write(buffer, 0, read);
  79.                 setDownloaded(getDownloaded()+read);
  80.  
  81.             }
  82.  
  83.             /*
  84.              * Change status to complete if this point was reached because
  85.              * downloading has finished.
  86.              */
  87.             if (getStatus() == DOWNLOADING) {
  88.                 setStatus(COMPLETE);
  89.  
  90.             }
  91.         } catch (Exception e) {
  92.             e.printStackTrace();
  93.             System.out.println("There is an Error!");
  94.         } finally {
  95.             // Close file.
  96.             if (rfile != null) {
  97.                 try {
  98.                     rfile.close();
  99.                 } catch (Exception e) {
  100.                 }
  101.             }
  102.  
  103.             // Close connection to server.
  104.             if (stream != null) {
  105.                 try {
  106.                     stream.close();
  107.                 } catch (Exception e) {
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
In UI of the project, I want to provide user to select download speed as Fast, Medium, Slow.How can I do this.I have also threadPool structure in my project in DownloadManager.java and it only takes 10 downloads to the pool.Is download speed related with it?Or how can I increase or decrease the speed of downloads?Any idea would be appreciated.
Apr 27 '13 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,422
You might be able to throttle the speed, but there's not much you can do to increase the speed. That's dependent on their ISP and your ISP, distance from the server, and the level of service you each have.
Apr 27 '13 #2

P: 28
@Rabbit How can I throttle the speed?
Apr 27 '13 #3

Nepomuk
Expert 2.5K+
P: 3,112
I don't think there's a simple way to do that (at least I'm not aware of one), so you'll probably have to dig into the HTTP and maybe even TCP protocol. In theory, UDP may be used too, but the combination of HTTP and UDP is rare.
The thing to look out for is reducing the speed at which incoming packages are accepted; I believe there was even a value you could set in one of the packet headers to tell the server how quickly stuff should be sent. But even if that's not the case, you should somehow be able to throttle the speed at which your client accepts packets. Any reasonably modern server will quickly adjust.
Apr 29 '13 #4

Expert 100+
P: 785
In your while loop you read the downloaded file slice by slice where a slice size is defined by MAX_BUFFER_SIZE. So to throttle down:
- decrease the size of MAX_BUFFER_SIZE (but only if you want to slow down very small files)
- After reading a slice, place a sleep-command (line 80 in your code): Thread.sleep(100); This will sleep for 100ms before reading the next slice. ( You can increase this number 100 as needed to slow down even more)
Apr 29 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.