diff --git a/src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java b/src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java index 92c5a2f19..b2de25446 100644 --- a/src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java +++ b/src/plugins/ftp/src/android/com/foxdebug/ftp/Ftp.java @@ -595,24 +595,33 @@ public void run() { ftp.setFileType(FTP.BINARY_FILE_TYPE); - InputStream inputStream = ftp.retrieveFileStream(path); - if (inputStream == null) { - Log.d( - "FTP", - "FTPClient (" + ftpId + ") path: " + path + " - not found" - ); - callback.error("File not found."); - return; + // Delete existing cache file to prevent stale content + if (localFile.exists()) { + localFile.delete(); } - FileOutputStream outputStream = new FileOutputStream(localFile); - byte[] buffer = new byte[1024]; - int bytesRead = -1; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); + try ( + InputStream inputStream = ftp.retrieveFileStream(path) + ) { + if (inputStream == null) { + Log.d( + "FTP", + "FTPClient (" + ftpId + ") path: " + path + " - not found" + ); + callback.error("File not found."); + return; + } + + try ( + FileOutputStream outputStream = new FileOutputStream(localFile) + ) { + byte[] buffer = new byte[1024]; + int bytesRead = -1; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } } - outputStream.close(); - inputStream.close(); if (!ftp.completePendingCommand()) { ftp.logout(); @@ -675,20 +684,22 @@ public void run() { ftp.setFileType(FTP.BINARY_FILE_TYPE); Log.d("FTPUpload", "Destination " + remoteFilePath); - OutputStream outputStream = ftp.storeFileStream(remoteFilePath); - if (outputStream == null) { - callback.error("File not found."); - return; - } - InputStream inputStream = new FileInputStream(localFile); - byte[] buffer = new byte[1024]; - int bytesRead = -1; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); + try ( + InputStream inputStream = new FileInputStream(localFile); + OutputStream outputStream = ftp.storeFileStream(remoteFilePath) + ) { + if (outputStream == null) { + callback.error("File not found."); + return; + } + + byte[] buffer = new byte[1024]; + int bytesRead = -1; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } } - outputStream.close(); - inputStream.close(); if (!ftp.completePendingCommand()) { ftp.logout(); diff --git a/src/plugins/sftp/src/com/foxdebug/sftp/Sftp.java b/src/plugins/sftp/src/com/foxdebug/sftp/Sftp.java index ce5f46687..25b373742 100644 --- a/src/plugins/sftp/src/com/foxdebug/sftp/Sftp.java +++ b/src/plugins/sftp/src/com/foxdebug/sftp/Sftp.java @@ -320,7 +320,7 @@ public void run() { try ( InputStream inputStream = sftp.getInputStream(filename); java.io.OutputStream outputStream = - contentResolver.openOutputStream(fileUri) + contentResolver.openOutputStream(fileUri, "wt") ) { byte[] buffer = new byte[32768]; int bytesRead; diff --git a/src/utils/polyfill.js b/src/utils/polyfill.js index 695c56ce2..11dd85f5e 100644 --- a/src/utils/polyfill.js +++ b/src/utils/polyfill.js @@ -47,13 +47,39 @@ export default function loadPolyFill() { if (!String.prototype.hashCode) { Object.defineProperty(String.prototype, "hashCode", { value: function () { - let hash = 0; - for (let i = 0; i < this.length; i++) { - const chr = this.charCodeAt(i); - hash = (hash << 5) - hash + chr; - hash |= 0; // Convert to 32bit integer + const str = this.toString(); + const len = str.length; + + if (len === 0) return "0"; + + // Produces a 48-char hex string (192 bits) + const FNV_PRIME = 0x01000193; + const FNV_OFFSET = 0x811c9dc5; + + // Generate 6 different 32-bit hashes with different seeds/offsets + const hashes = []; + for (let pass = 0; pass < 6; pass++) { + let hash = FNV_OFFSET ^ (pass * 0x1234567); + + for (let i = 0; i < len; i++) { + const char = str.charCodeAt(i); + // XOR with byte and multiply by prime + hash ^= char; + hash = Math.imul(hash, FNV_PRIME); + // Mix in position and pass for additional entropy + hash ^= (i + pass) & 0xff; + hash = Math.imul(hash, FNV_PRIME); + } + + // Additional mixing + hash ^= len; + hash = Math.imul(hash, FNV_PRIME); + hash ^= hash >>> 16; + + hashes.push((hash >>> 0).toString(16).padStart(8, "0")); } - return Math.abs(hash) + (hash < 0 ? "N" : ""); + + return hashes.join(""); }, }); }