Skip to content

Commit 5630219

Browse files
fix: resource leak
1 parent 4cac5b3 commit 5630219

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/plugins/auth/src/android/PluginRetriever.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ private static String withSupportedEditor(String url) {
2626

2727

2828
public static void downloadPlugin(String token, String pluginUrl, String destFile, CallbackContext callbackContext) {
29+
HttpURLConnection connection = null;
30+
2931
try {
3032
// Strip file:// prefix if present
3133
if (destFile.startsWith("file://")) {
3234
destFile = destFile.substring(7);
3335
}
3436

3537
URL url = new URL(pluginUrl);
36-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
38+
connection = (HttpURLConnection) url.openConnection();
3739
connection.setRequestMethod("GET");
40+
connection.setConnectTimeout(15000);
41+
connection.setReadTimeout(30000);
3842

3943
if (token != null && !token.isEmpty()) {
4044
if (url.getHost().endsWith("acode.app")) {
@@ -46,16 +50,21 @@ public static void downloadPlugin(String token, String pluginUrl, String destFil
4650

4751
connection.connect();
4852

49-
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
50-
callbackContext.error("HTTP error: " + connection.getResponseCode());
53+
int responseCode = connection.getResponseCode();
54+
if (responseCode != HttpURLConnection.HTTP_OK) {
55+
callbackContext.error("HTTP error: " + responseCode);
5156
return;
5257
}
5358

5459
File tempFile = new File(destFile);
55-
try (InputStream inputStream = connection.getInputStream();
56-
FileOutputStream outputStream = new FileOutputStream(tempFile)) {
60+
61+
try (
62+
InputStream inputStream = connection.getInputStream();
63+
FileOutputStream outputStream = new FileOutputStream(tempFile)
64+
) {
5765
byte[] buffer = new byte[4096];
5866
int bytesRead;
67+
5968
while ((bytesRead = inputStream.read(buffer)) != -1) {
6069
outputStream.write(buffer, 0, bytesRead);
6170
}
@@ -65,6 +74,11 @@ public static void downloadPlugin(String token, String pluginUrl, String destFil
6574

6675
} catch (Exception e) {
6776
callbackContext.error("Download failed: " + e.getMessage());
77+
78+
} finally {
79+
if (connection != null) {
80+
connection.disconnect();
81+
}
6882
}
6983
}
7084

0 commit comments

Comments
 (0)