@@ -595,24 +595,33 @@ public void run() {
595595
596596 ftp .setFileType (FTP .BINARY_FILE_TYPE );
597597
598- InputStream inputStream = ftp .retrieveFileStream (path );
599- if (inputStream == null ) {
600- Log .d (
601- "FTP" ,
602- "FTPClient (" + ftpId + ") path: " + path + " - not found"
603- );
604- callback .error ("File not found." );
605- return ;
598+ // Delete existing cache file to prevent stale content
599+ if (localFile .exists ()) {
600+ localFile .delete ();
606601 }
607602
608- FileOutputStream outputStream = new FileOutputStream (localFile );
609- byte [] buffer = new byte [1024 ];
610- int bytesRead = -1 ;
611- while ((bytesRead = inputStream .read (buffer )) != -1 ) {
612- outputStream .write (buffer , 0 , bytesRead );
603+ try (
604+ InputStream inputStream = ftp .retrieveFileStream (path )
605+ ) {
606+ if (inputStream == null ) {
607+ Log .d (
608+ "FTP" ,
609+ "FTPClient (" + ftpId + ") path: " + path + " - not found"
610+ );
611+ callback .error ("File not found." );
612+ return ;
613+ }
614+
615+ try (
616+ FileOutputStream outputStream = new FileOutputStream (localFile )
617+ ) {
618+ byte [] buffer = new byte [1024 ];
619+ int bytesRead = -1 ;
620+ while ((bytesRead = inputStream .read (buffer )) != -1 ) {
621+ outputStream .write (buffer , 0 , bytesRead );
622+ }
623+ }
613624 }
614- outputStream .close ();
615- inputStream .close ();
616625
617626 if (!ftp .completePendingCommand ()) {
618627 ftp .logout ();
@@ -675,20 +684,22 @@ public void run() {
675684 ftp .setFileType (FTP .BINARY_FILE_TYPE );
676685
677686 Log .d ("FTPUpload" , "Destination " + remoteFilePath );
678- OutputStream outputStream = ftp .storeFileStream (remoteFilePath );
679- if (outputStream == null ) {
680- callback .error ("File not found." );
681- return ;
682- }
683687
684- InputStream inputStream = new FileInputStream (localFile );
685- byte [] buffer = new byte [1024 ];
686- int bytesRead = -1 ;
687- while ((bytesRead = inputStream .read (buffer )) != -1 ) {
688- 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+ }
689702 }
690- outputStream .close ();
691- inputStream .close ();
692703
693704 if (!ftp .completePendingCommand ()) {
694705 ftp .logout ();
0 commit comments