Switched URL to a URI for better mapping
package org.terasology.utilities.download;
import java.io.IOException;
+import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.Callable;
+/**
+ * Downloads multiple files.
+ */
+
public class MultiFileDownloader implements Callable<List<Path>> {
- private Map<URL, Path> urlToTargetMap;
+ private Map<URI, Path> uriToTargetMap;
private MultiFileTransferProgressListener progressListener;
- public MultiFileDownloader(Map<URL, Path> urlToTargetMap, MultiFileTransferProgressListener progressListener) {
- this.urlToTargetMap = urlToTargetMap;
+ /**
+ * @param uriToTargetMap
+ * @param progressListener a progress listener. Will be called with 0 repeatedly if size cannot be determined.
+ */
+ public MultiFileDownloader(Map<URI, Path> uriToTargetMap, MultiFileTransferProgressListener progressListener) {
+ this.uriToTargetMap = uriToTargetMap;
this.progressListener = progressListener;
}
@@ -39,22 +48,25 @@ public MultiFileDownloader(Map<URL, Path> urlToTargetMap, MultiFileTransferProgr
public List<Path> call() throws IOException {
// Obtain the size of each file
- final int numFiles = urlToTargetMap.size();
+ final int numFiles = uriToTargetMap.size();
if (numFiles < 1) { // avoid reporting progress like "Retrieving file size 1 of 0"
return Collections.emptyList();
}
- final Map<URL, Long> fileSizes = getSizes();
+ final Map<URI, Long> fileSizes = getSizes();
final long totalBytes = sum(fileSizes.values());
List<Path> downloadedFiles = new ArrayList<>();
int index = 0;
long completedFilesBytes = 0;
- for (Map.Entry<URL, Path> entry : urlToTargetMap.entrySet()) {
+ for (Map.Entry<URI, Path> entry : uriToTargetMap.entrySet()) {
final int downloadedFileCount = index + 1;
final long previousFilesBytes = completedFilesBytes;
SingleFileTransferProgressListener singleDownloadListener = (fileDownloadedBytes, fileTotalBytes) ->
progressListener.onDownloadProgress(previousFilesBytes + fileDownloadedBytes, totalBytes, downloadedFileCount, numFiles);
- SingleFileDownloader fileDownloader = new SingleFileDownloader(entry.getKey(), entry.getValue(),
+
+ // Converting URI to URL in order to pass it SingleFileDownloader's constructor
+ URL url = entry.getKey().toURL();
+ SingleFileDownloader fileDownloader = new SingleFileDownloader(url, entry.getValue(),
singleDownloadListener);
downloadedFiles.add(fileDownloader.call());
completedFilesBytes += fileSizes.get(entry.getKey());
@@ -62,15 +74,14 @@ public MultiFileDownloader(Map<URL, Path> urlToTargetMap, MultiFileTransferProgr
}
return downloadedFiles;
}
-
- private Map<URL, Long> getSizes() throws IOException {
- Map<URL, Long> result = new HashMap<>();
+ private Map<URI, Long> getSizes() throws IOException {
+ Map<URI, Long> result = new HashMap<>();
int current = 1;
- int total = urlToTargetMap.size();
- for (URL url: urlToTargetMap.keySet()) {
+ int total = uriToTargetMap.size();
+ for (URI uri: uriToTargetMap.keySet()) {
progressListener.onSizeMetadataProgress(current, total);
- URLConnection conn = url.openConnection();
- result.put(url, conn.getContentLengthLong());
+ URLConnection connection = uri.toURL().openConnection();
+ result.put(uri, connection.getContentLengthLong());
current++;
}
return result;
