Main.java: entry point that triggers a download.FileDownloader.java: orchestrates the download (HEAD request, chunk calculation, scheduling parallel downloads, combining results).ChunkDownloader.java:Callable<byte[]>that downloads one specific byte range.FileDownloaderTest.java: unit tests for file size detection, chunk calculation, and chunk combination.
-
Discover file size (HEAD request)
FileDownloader#getFileSizesends an HTTPHEADrequest to the provided URL and reads theContent-Lengthheader to determine the total size of the file in bytes. The server should returnAccept-Ranges: bytes, indicating support for range requests. -
Split into chunks
Based on the file size and configured number of threads,FileDownloader#calculateChunksdivides the file into contiguous ranges[start, end]so that every byte in the file is covered exactly once and there are no gaps or overlaps. -
Download chunks in parallel
FileDownloader#downloadcreates a fixed-size thread pool and submits aChunkDownloadertask for each range. EachChunkDownloader:- sends an HTTP
GETrequest to the same URL - adds the header
Range: bytes=startByte-endByte - returns the response body as a
byte[]
- sends an HTTP
-
Combine chunks into the final file
Once all futures complete,FileDownloader#combineChunksconcatenates thebyte[]results in the correct order and writes the final byte array to the output file on disk.
The project includes unit tests that validate:
- reading
Content-Lengthfrom aHEADresponse (getFileSize) - splitting a file size into correct chunks (
calculateChunks) - correct concatenation of byte arrays (
combineChunks) - error handling when the server returns a non-200 status