473,666 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

28 New Member
Here is my DownloadFile.ja va.
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 5325
Rabbit
12,516 Recognized Expert Moderator MVP
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
notfound
28 New Member
@Rabbit How can I throttle the speed?
Apr 27 '13 #3
Nepomuk
3,112 Recognized Expert Specialist
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 Recognized Expert Contributor
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(10 0); 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
1595
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 that I could implement to get over this problem? I am relatively new to databases, but have programming in java for a while.
3
2186
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
6161
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 the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip 2000 Interview questions of .NET , JAVA and SQL Server Interview questions (worth downloading it)
2
6948
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 attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
18
1945
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 think the problem is at line #1, #2, #3. import wave import array lfile = wave.open(lfilename) rfile = wave.open(rfilename)
6
2435
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 any of you send me an implementation code of the process.java thnkx...
1
1357
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 page in my system, but when i try to run the program from another system, the page gets downloaded to my system, not on the system from which i called the program.. ie its running in the server,but not coming to the client side.... how can i solve...
10
11723
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
8449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8876
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8784
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.