Skip to content

Commit f7e31af

Browse files
committed
feat(window): 使用流代替原有方法,强化取消按钮打断能力
1 parent 3fd89c5 commit f7e31af

3 files changed

Lines changed: 54 additions & 4 deletions

File tree

src/main/java/i18nupdatemod/I18nUpdateMod.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public static void init(Path minecraftPath, String minecraftVersion, String load
3434
LoadDetailUI.setStage(LoadStage.INIT);
3535

3636
try (InputStream is = I18nUpdateMod.class.getResourceAsStream("/i18nMetaData.json")) {
37+
if (is == null) {
38+
throw new IllegalStateException("/i18nMetaData.json not found");
39+
}
3740
MOD_VERSION = GSON.fromJson(new InputStreamReader(is), JsonObject.class).get("version").getAsString();
3841
} catch (Exception e) {
3942
Log.warning("Error getting version: " + e);

src/main/java/i18nupdatemod/core/ResourcePack.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package i18nupdatemod.core;
22

3+
import i18nupdatemod.I18nUpdateMod;
34
import i18nupdatemod.util.AssetUtil;
45
import i18nupdatemod.util.DigestUtil;
56
import i18nupdatemod.util.FileUtil;
67
import i18nupdatemod.util.Log;
78

89
import java.io.FileNotFoundException;
910
import java.io.IOException;
11+
import java.io.InterruptedIOException;
1012
import java.net.URISyntaxException;
1113
import java.nio.file.Files;
1214
import java.nio.file.Path;
@@ -78,17 +80,29 @@ private boolean checkMd5(Path localFile, String md5Url) throws IOException, URIS
7880
}
7981

8082
private void downloadFull(String fileUrl, String md5Url) throws IOException {
83+
if (I18nUpdateMod.shouldShutdown) {
84+
throw new InterruptedIOException("Download cancelled by user");
85+
}
86+
87+
Path downloadTmp = FileUtil.getTemporaryPath(filename + ".tmp");
8188
try {
82-
Path downloadTmp = FileUtil.getTemporaryPath(filename + ".tmp");
8389
AssetUtil.download(fileUrl, downloadTmp);
8490
if (!checkMd5(downloadTmp, md5Url)) {
8591
throw new IOException("Download MD5 not match");
8692
}
8793
Files.move(downloadTmp, tmpFilePath, StandardCopyOption.REPLACE_EXISTING);
8894
Log.debug(String.format("Updates temp file: %s", tmpFilePath));
95+
} catch (InterruptedIOException e) {
96+
Files.deleteIfExists(downloadTmp);
97+
throw e;
8998
} catch (Exception e) {
9099
Log.warning("Error while downloading: %s", e);
91100
}
101+
102+
if (I18nUpdateMod.shouldShutdown) {
103+
throw new InterruptedIOException("Download cancelled by user");
104+
}
105+
92106
if (!Files.exists(tmpFilePath)) {
93107
throw new FileNotFoundException("Tmp file not found.");
94108
}

src/main/java/i18nupdatemod/util/AssetUtil.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.reflect.TypeToken;
5-
import org.apache.commons.io.FileUtils;
5+
import i18nupdatemod.I18nUpdateMod;
66
import org.apache.commons.io.IOUtils;
77
import org.jetbrains.annotations.NotNull;
88

99
import java.io.IOException;
10+
import java.io.InputStream;
1011
import java.io.InputStreamReader;
12+
import java.io.InterruptedIOException;
13+
import java.io.OutputStream;
1114
import java.lang.reflect.Type;
1215
import java.net.HttpURLConnection;
1316
import java.net.URI;
1417
import java.net.URISyntaxException;
1518
import java.net.URL;
19+
import java.net.SocketTimeoutException;
1620
import java.nio.charset.StandardCharsets;
21+
import java.nio.file.Files;
1722
import java.nio.file.Path;
1823
import java.util.ArrayList;
1924
import java.util.HashMap;
@@ -35,8 +40,36 @@ public class AssetUtil {
3540

3641
public static void download(String url, Path localFile) throws IOException, URISyntaxException {
3742
Log.info("Downloading: %s -> %s", url, localFile);
38-
FileUtils.copyURLToFile(new URI(url).toURL(), localFile.toFile(),
39-
(int) TimeUnit.SECONDS.toMillis(3), (int) TimeUnit.SECONDS.toMillis(33));
43+
44+
HttpURLConnection connection = (HttpURLConnection) new URI(url).toURL().openConnection();
45+
connection.setRequestMethod("GET");
46+
connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(3));
47+
connection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(1));
48+
49+
try (InputStream input = connection.getInputStream();
50+
OutputStream output = Files.newOutputStream(localFile)) {
51+
byte[] buffer = new byte[8192];
52+
while (true) {
53+
if (I18nUpdateMod.shouldShutdown) {
54+
throw new InterruptedIOException("Download cancelled by user");
55+
}
56+
57+
int read;
58+
try {
59+
read = input.read(buffer);
60+
} catch (SocketTimeoutException e) {
61+
continue;
62+
}
63+
64+
if (read < 0) {
65+
break;
66+
}
67+
output.write(buffer, 0, read);
68+
}
69+
} finally {
70+
connection.disconnect();
71+
}
72+
4073
Log.debug("Downloaded: %s -> %s", url, localFile);
4174
}
4275

0 commit comments

Comments
 (0)