Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit 6eb830b

Browse files
authored
Fix(DownloadUtils): Set Read Timeout Duration (#6504)
* Fix[DownloadUtils]: Set Read Timeout Duration Pojav has not set a timeout for downloading files, which results in situations where file downloads do not throw an exception or terminate even after timing out. * Update DownloadUtils.java
1 parent 42c1c98 commit 6eb830b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/DownloadUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
@SuppressWarnings("IOStreamConstructor")
1616
public class DownloadUtils {
1717
public static final String USER_AGENT = Tools.APP_NAME;
18+
private static final int TIME_OUT = 10000;
1819

1920
public static void download(String url, OutputStream os) throws IOException {
2021
download(new URL(url), os);
@@ -26,7 +27,8 @@ public static void download(URL url, OutputStream os) throws IOException {
2627
// System.out.println("Connecting: " + url.toString());
2728
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
2829
conn.setRequestProperty("User-Agent", USER_AGENT);
29-
conn.setConnectTimeout(10000);
30+
conn.setConnectTimeout(TIME_OUT);
31+
conn.setReadTimeout(TIME_OUT);
3032
conn.setDoInput(true);
3133
conn.connect();
3234
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
@@ -67,6 +69,8 @@ public static void downloadFileMonitored(String urlInput, File outputFile, @Null
6769
FileUtils.ensureParentDirectory(outputFile);
6870

6971
HttpURLConnection conn = (HttpURLConnection) new URL(urlInput).openConnection();
72+
conn.setConnectTimeout(TIME_OUT);
73+
conn.setReadTimeout(TIME_OUT);
7074
InputStream readStr = conn.getInputStream();
7175
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
7276
int current;
@@ -81,8 +85,9 @@ public static void downloadFileMonitored(String urlInput, File outputFile, @Null
8185
monitor.updateProgress(overall, length);
8286
}
8387
conn.disconnect();
88+
} catch (IOException e) {
89+
throw new IOException("Unable to download from " + urlInput, e);
8490
}
85-
8691
}
8792

8893
public static <T> T downloadStringCached(String url, String cacheName, ParseCallback<T> parseCallback) throws IOException, ParseException{

0 commit comments

Comments
 (0)