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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • notfound
    New Member
    • Jun 2012
    • 28

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

    Here is my DownloadFile.ja va.
    Code:
    public class DownloadFile extends Download implements Runnable, DownloadFileInterface {
    	
    	private String name; //get name of the file
    	
    	public DownloadFile(URL url, int id) {
    		super(url, id);
    		
    	}
    
    	// Start downloading.
    	public void download() {
    		System.out.println("Downloading...");
    		InputStream stream = null;
    		File file = new File(getUrl());
    		RandomAccessFile rfile = null;
    		System.out.println("Download started: " + getId());
            setName(file.getFileName());
    		try {
    
    			// Open connection to URL.
    			HttpURLConnection connection = (HttpURLConnection) getUrl()
    					.openConnection();
    
    			// Specify what portion of file to download.
    			connection.setRequestProperty("Range", "bytes=" + getDownloaded() + "-");
    
    			// Connect to server.
    			connection.connect();
    
    			int contentLength = connection.getContentLength();
    			System.out.println(contentLength);
    
    			/*
    			 * Set the size for this download if it hasn't been already set.
    			 */
    			if (getSize() == -1) {
    				setSize(contentLength);
    
    			}
    
    			// Open file and seek to the end of it.
    			rfile = file.openRandomAccessFile();
    			rfile.seek(getDownloaded());
    
    			stream = connection.getInputStream();
    
    			while (getStatus() == DOWNLOADING) {
    				/*
    				 * Size buffer according to how much of the file is left to
    				 * download.
    				 */
    				byte buffer[];
    
    				if (getSize() - getDownloaded() > MAX_BUFFER_SIZE) {
    					buffer = new byte[MAX_BUFFER_SIZE];
    					// System.out.println("id: " + id + " downloaded byte: " +
    					// MAX_BUFFER_SIZE);
    				} else {
    					int remainingByte = getSize() - getDownloaded();
    					if (remainingByte <= 0) {
    
    						System.out.println(file.getFileName()
    								+ " has been downloaded");
    						break;
    					}
    
    					else {
    						buffer = new byte[remainingByte];
    						// System.out.println("id: " + id + " remaining byte: "
    						// + remainingByte);
    					}
    				}
    
    				// Read from server into buffer.
    				int read = stream.read(buffer);
    
    				// Write buffer to file.
    				rfile.write(buffer, 0, read);
    				setDownloaded(getDownloaded()+read);
    
    			}
    
    			/*
    			 * Change status to complete if this point was reached because
    			 * downloading has finished.
    			 */
    			if (getStatus() == DOWNLOADING) {
    				setStatus(COMPLETE);
    
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    			System.out.println("There is an Error!");
    		} finally {
    			// Close file.
    			if (rfile != null) {
    				try {
    					rfile.close();
    				} catch (Exception e) {
    				}
    			}
    
    			// Close connection to server.
    			if (stream != null) {
    				try {
    					stream.close();
    				} catch (Exception e) {
    				}
    			}
    		}
    	}
    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.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • notfound
      New Member
      • Jun 2012
      • 28

      #3
      @Rabbit How can I throttle the speed?

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        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.

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          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)

          Comment

          Working...