0

I'm working on an app that requires downloading of big video files (300-500MB).
I would like to know if there's a way to speed up the download of a single file using multi-threading.
I've saw this type of implementation on applications like TubeMate, but I clueless about the code used to achieve it.
I don't need you to code for me :) I just need a starting point, like an article or tutorial about this, tks.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 2
    yes, if your server supports it, you can multi-thread it using the `Range` header. It requires to reconstruct the file a posteriori, though. I doubt it would really speed up the download, though. I would start by identifying the bottleneck in the download process. – njzk2 Mar 04 '14 at 21:07
  • @njzk2 any article about the `Range Header and reconstruct the file a posteriori` that you can share? – Pedro Lobito Mar 04 '14 at 21:09
  • 1
    I agree with @njzk2. Most of the magic would be handled by the server, so on the applications side it doesn't seem that difficult (keep track of the chunks to reconstruct later). Improvements will occur only if the server has a bandwidth threshold per connection. – Merlevede Mar 04 '14 at 21:09
  • 1
    @Tuga: not really, but the basic idea is quite simple: using `Range`, you ask the server for specific chunks of the file. You save them somewhere, then cat then in one file in the correct order. – njzk2 Mar 04 '14 at 21:24
  • @njzk2 is the range request valid for all servers ? – Pedro Lobito Mar 04 '14 at 21:29
  • @Tuga: not necessarily. But it is quite common for servers that serve large files, as it allows partial downloads and download resuming. – njzk2 Mar 04 '14 at 21:31

2 Answers2

2

Video can be represented as byte array. If you want to load array faster you can divide it in parts and load every part in separate thread. The best way to do that is using ThreadPoolExecutor to run the task and FutureTask to retrieve results. In case you need to dawnload video from the internet, your server should support api calls that can give you "part" of video. Here is the good sample of how to use Executors and Futures: https://www.journaldev.com/1650/java-futuretask-example-program

1

Regardless of finding a library function, you could also make your own script to handle partial downloads.

For example, in PHP, you can use the fopen(), fread()... functions to open a stream on the server, seek the file to a particular position and transmit bytes to the requester. File offset and chunk length parameters would be set by the client using POST or GET methods.

Check this link. Maybe it helps you a little bit.

If, on the other hand, you don't have access to the server, and you just want to improve downloads from any Internet site, then you could look up for HTTP range header or here.

Community
  • 1
  • 1
Merlevede
  • 8,140
  • 1
  • 24
  • 39