diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadManager.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadManager.java new file mode 100644 index 000000000..8a5c851ea --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadManager.java @@ -0,0 +1,137 @@ +package org.jackhuang.hmcl.task; + +import com.google.gson.JsonParseException; +import org.jackhuang.hmcl.util.gson.JsonUtils; +import org.jackhuang.hmcl.util.io.FileUtils; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +class DownloadManager { + + static DownloadState ne(int contentLength, int initialParts) { + + } + + static DownloadTaskState download(List urls, Path file, int initialParts) throws IOException { + Path downloadingFile = file.resolveSibling(FileUtils.getName(file) + ".download"); + Path stateFile = file.resolveSibling(FileUtils.getName(file) + ".status"); + DownloadState state; + if (Files.exists(downloadingFile) && Files.exists(stateFile)) { + // Resume downloading from state + try { + String status = FileUtils.readText(stateFile); + state = JsonUtils.fromNonNullJson(status, DownloadState.class); + } catch (JsonParseException e) { + state = + } + } + } + + protected static class DownloadTaskState { + private final List urls; + private final List segments; + + DownloadTaskState(DownloadState state) { + urls = new ArrayList<>(state.urls); + segments = new ArrayList<>(state.segments); + } + + DownloadTaskState(List urls, int contentLength, int initialParts) { + urls = new ArrayList<>(urls); + segments = new ArrayList<>(initialParts); + int partLength = contentLength / initialParts; + for (int i = 0; i < initialParts; i++) { + int begin = partLength * i; + int end = Math.min((partLength + 1) * i, contentLength); + segments.add(new DownloadSegment(begin, end, 0)); + } + } + + public static DownloadTaskState newWithLengthUnknown(List urls, int initialParts) { + return + } + + public List getUrls() { + return urls; + } + + public List getSegments() { + return segments; + } + } + + protected static class DownloadState { + private final List urls; + private final List segments; + + /** + * Constructor for Gson + */ + public DownloadState() { + this(Collections.emptyList(), Collections.emptyList()); + } + + public DownloadState(List urls, List segments) { + this.urls = urls; + this.segments = segments; + } + + public List getUrls() { + return urls; + } + + public List getSegments() { + return segments; + } + } + + protected static class DownloadSegment { + private final int startPosition; + private final int endPosition; + private int currentPosition; + + /** + * Constructor for Gson + */ + public DownloadSegment() { + this(0, 0, 0); + } + + public DownloadSegment(int startPosition, int endPosition, int currentPosition) { + if (currentPosition < startPosition || currentPosition > endPosition) { + throw new IllegalArgumentException("Illegal download state: start " + startPosition + ", end " + endPosition + ", cur " + currentPosition); + } + this.startPosition = startPosition; + this.endPosition = endPosition; + this.currentPosition = currentPosition; + } + + public int getStartPosition() { + return startPosition; + } + + public int getEndPosition() { + return endPosition; + } + + public int getCurrentPosition() { + return currentPosition; + } + + public void setCurrentPosition(int currentPosition) { + this.currentPosition = currentPosition; + } + + public boolean isFinished() { + return currentPosition == endPosition; + } + + public boolean isWaiting() { return startPosition == endPosition && startPosition == 0; } + } + +} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadTask.java new file mode 100644 index 000000000..ffe71c6b6 --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/DownloadTask.java @@ -0,0 +1,5 @@ +package org.jackhuang.hmcl.task; + +class DownloadTask { + +} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java index ecc1041f6..11f78d3c8 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java @@ -226,39 +226,6 @@ public abstract class FetchTask extends Task { NOT_CHECK_E_TAG, CACHED } - - protected class DownloadState { - private final int startPosition; - private final int endPosition; - private final int currentPosition; - private final boolean finished; - - public DownloadState(int startPosition, int endPosition, int currentPosition) { - if (currentPosition < startPosition || currentPosition > endPosition) { - throw new IllegalArgumentException("Illegal download state: start " + startPosition + ", end " + endPosition + ", cur " + currentPosition); - } - this.startPosition = startPosition; - this.endPosition = endPosition; - this.currentPosition = currentPosition; - finished = currentPosition == endPosition; - } - - public int getStartPosition() { - return startPosition; - } - - public int getEndPosition() { - return endPosition; - } - - public int getCurrentPosition() { - return currentPosition; - } - - public boolean isFinished() { - return finished; - } - } protected class DownloadMission {