Skip to content

Commit df31809

Browse files
committed
fix: resource leak
1 parent 81612b0 commit df31809

File tree

1 file changed

+14
-12
lines changed
  • src/plugins/ftp/src/android/com/foxdebug/ftp

1 file changed

+14
-12
lines changed

src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -684,20 +684,22 @@ public void run() {
684684
ftp.setFileType(FTP.BINARY_FILE_TYPE);
685685

686686
Log.d("FTPUpload", "Destination " + remoteFilePath);
687-
OutputStream outputStream = ftp.storeFileStream(remoteFilePath);
688-
if (outputStream == null) {
689-
callback.error("File not found.");
690-
return;
691-
}
692687

693-
InputStream inputStream = new FileInputStream(localFile);
694-
byte[] buffer = new byte[1024];
695-
int bytesRead = -1;
696-
while ((bytesRead = inputStream.read(buffer)) != -1) {
697-
outputStream.write(buffer, 0, bytesRead);
688+
try (
689+
InputStream inputStream = new FileInputStream(localFile);
690+
OutputStream outputStream = ftp.storeFileStream(remoteFilePath)
691+
) {
692+
if (outputStream == null) {
693+
callback.error("File not found.");
694+
return;
695+
}
696+
697+
byte[] buffer = new byte[1024];
698+
int bytesRead = -1;
699+
while ((bytesRead = inputStream.read(buffer)) != -1) {
700+
outputStream.write(buffer, 0, bytesRead);
701+
}
698702
}
699-
outputStream.close();
700-
inputStream.close();
701703

702704
if (!ftp.completePendingCommand()) {
703705
ftp.logout();

0 commit comments

Comments
 (0)