Results 1 to 4 of 4
  1. #1
    pietertje is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default How to make my download class faster

    Hi,

    Currently I'm working on a small downloadmanager for personal use, it's working fine so far, except that it downloads rather slowly. My downloadmanager averages on about 170~ kb/s when downloading file, but when I download the same file regularly it downloads at 640~ kb/s.

    My download function:
    Java Code:
    	public void Download() {
    		
    		try {
    
    			HttpURLConnection huc = this.Gethuc(this.FileLink + "?directstart=1");
    			InputStream is = huc.getInputStream();
    			FileOutputStream fos = new FileOutputStream(this.GetLocalFile());
    			this.SetFileSize(huc.getContentLength());
    
    			int SingleByte;
    			
    			while ((SingleByte=is.read()) != -1) {
    				
    				fos.write(SingleByte);
    				
    			}
    			
    			is.close();
    			fos.close();
    		
    		} catch (MalformedURLException e) {
    			System.err.println(e.toString());
    		} catch (IOException e) {
    			System.err.println(e.toString());
    		}
    	}
    Does anyone know a way to make it faster?

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default

    Try reading thousands of bytes at a time vs only 1

  3. #3
    pietertje is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Ye I fixed it by reading into a buffer. So basically what you said.

    Java Code:
    	public void Download() {
    		
    		try {
    
    			HttpURLConnection huc = this.Gethuc(this.FileLink + "?directstart=1");
    			InputStream is = huc.getInputStream();
    			FileOutputStream fos = new FileOutputStream(this.GetLocalFile());
    			this.SetFileSize(huc.getContentLength());
    			
    			final int bufferSize = 1024 * 512;
    			byte[] buffer = new byte[bufferSize];
    			int bytesRead;
    			long Time = System.currentTimeMillis();
    			
    			while ((bytesRead = is.read(buffer)) > 0) {
    				
    				fos.write(buffer, 0, bytesRead);
    				
    			}
    			System.out.println((System.currentTimeMillis() - Time)/1000);
    			is.close();
    			fos.close();
    		
    		} catch (MalformedURLException e) {
    			System.err.println(e.toString());
    		} catch (IOException e) {
    			System.err.println(e.toString());
    		}
    	}

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pietertje View Post
    Ye I fixed it by reading into a buffer. So basically what you said.

    Java Code:
    			final int bufferSize = 1024 * 512;
    Not that it matters much because any computer can easily handle a buffer size like that but normally the internal buffer size used by disk reads is 4KB or 8KB and is enough to accomplish a high throughput; try it and see for yourself.

    kind regards,

    Jos

Similar Threads

  1. How to make swing.Timer as a separate class
    By nethz13 in forum New To Java
    Replies: 9
    Last Post: 04-18-2010, 09:14 AM
  2. Hints on how to make a Java Class
    By luron31 in forum New To Java
    Replies: 11
    Last Post: 07-09-2009, 05:31 AM
  3. Replies: 2
    Last Post: 10-06-2008, 08:20 PM
  4. Please help, need to make my class static.
    By sumak in forum New To Java
    Replies: 1
    Last Post: 04-19-2008, 07:29 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
  •