Results 1 to 4 of 4
- 08-06-2010, 11:54 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
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:
Does anyone know a way to make it faster?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()); } }
Thanks in advance.
- 08-07-2010, 01:43 AM #2
Try reading thousands of bytes at a time vs only 1
- 08-07-2010, 10:35 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
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()); } }
- 08-07-2010, 11:19 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
How to make swing.Timer as a separate class
By nethz13 in forum New To JavaReplies: 9Last Post: 04-18-2010, 09:14 AM -
Hints on how to make a Java Class
By luron31 in forum New To JavaReplies: 11Last Post: 07-09-2009, 05:31 AM -
[SOLVED] Parallel/Asynchronous Faster indexing way on lucene
By priyanka.dandekar in forum LuceneReplies: 2Last Post: 10-06-2008, 08:20 PM -
Please help, need to make my class static.
By sumak in forum New To JavaReplies: 1Last Post: 04-19-2008, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks