473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
4 5291
Rabbit
12,516 Expert Mod 8TB
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
@Rabbit How can I throttle the speed?
Apr 27 '13 #3
Nepomuk
3,112 Expert 2GB
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
chaarmann
785 Expert 512MB
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

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

Similar topics

3
by: dufffman | last post by:
Hi, We essentially have (about 1000/sec) messages coming through a channel and I have to store them in a UDB database. Will UDB be able to write it that fast? If not what are a few strategies...
3
by: Crouchie1998 | last post by:
How do I work out the download speed in KB/Sec & how many hours/mins & seconds left? Thanks in advance for any help
0
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
18
by: HYRY | last post by:
I want to join two mono wave file to a stereo wave file by only using the default python module. Here is my program, but it is much slower than the C version, so how can I increase the speed? I...
6
by: chalani | last post by:
Hi... I was unable to find the implementation of process.java. What i need is the implementation of waitFor() method of process.java. but at every place what i find is the abstract class. could...
1
by: ajaymohank | last post by:
Hello friends......... i am new to php. In my project i have to download a java page from a bat file, to the client side by passing values from the program. the problem is that i am getting the java...
10
by: Devang | last post by:
Hello, I am using php script to upload file. some times if file size is too big(1GB) it takes too much time to upload. Can someone suggest me the way to increase upload speed. thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.