Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 8 additions & 32 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 27 additions & 9 deletions src/lib/editorFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,26 +610,44 @@ export default class EditorFile {
}

const protocol = Url.getProtocol(this.#uri);
let fs;
const text = this.session.getValue();

if (/s?ftp:/.test(protocol)) {
// if file is a ftp or sftp file, get file content from cached file.
// remove ':' from protocol because cache file of remote files are
// stored as ftp102525465N i.e. protocol + id
// Cache files are local file:// URIs, so native file reading works
const cacheFilename = protocol.slice(0, -1) + this.id;
const cacheFile = Url.join(CACHE_STORAGE, cacheFilename);
fs = fsOperation(cacheFile);
} else {
fs = fsOperation(this.uri);
const cacheFileUri = Url.join(CACHE_STORAGE, cacheFilename);

try {
return await system.compareFileText(cacheFileUri, this.encoding, text);
} catch (error) {
console.error("Native compareFileText failed:", error);
return false;
}
}

if (/^(file|content):/.test(protocol)) {
// file:// and content:// URIs can be handled by native Android code
// Native reads file AND compares in background thread
try {
return await system.compareFileText(this.uri, this.encoding, text);
} catch (error) {
console.error("Native compareFileText failed:", error);
return false;
}
}

// native compares
try {
const fs = fsOperation(this.uri);
const oldText = await fs.readFile(this.encoding);
const text = this.session.getValue();

if (oldText.length !== text.length) return true;
return oldText !== text;
// Offload string comparison to background thread
return await system.compareTexts(oldText, text);
} catch (error) {
window.log("error", error);
console.error(error);
return false;
}
}
Expand Down
139 changes: 139 additions & 0 deletions src/plugins/system/android/com/foxdebug/system/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public boolean execute(
case "decode":
case "encode":
case "copyToUri":
case "compare-file-text":
case "compare-texts":
break;
case "get-configuration":
getConfiguration(callbackContext);
Expand Down Expand Up @@ -505,6 +507,12 @@ public void run() {
case "encode":
encode(arg1, arg2, callbackContext);
break;
case "compare-file-text":
compareFileText(arg1, arg2, arg3, callbackContext);
break;
case "compare-texts":
compareTexts(arg1, arg2, callbackContext);
break;
default:
break;
}
Expand Down Expand Up @@ -616,6 +624,137 @@ private void encode(
}
}

/**
* Compares file content with provided text.
* This method runs in a background thread to avoid blocking the UI.
*
* @param fileUri The URI of the file to read (file:// or content://)
* @param encoding The character encoding to use when reading the file
* @param currentText The text to compare against the file content
* @param callback Returns 1 if texts are different, 0 if same
*/
private void compareFileText(
String fileUri,
String encoding,
String currentText,
CallbackContext callback
) {
try {
if (fileUri == null || fileUri.isEmpty()) {
callback.error("File URI is required");
return;
}

if (encoding == null || encoding.isEmpty()) {
encoding = "UTF-8";
}

if (!Charset.isSupported(encoding)) {
callback.error("Charset not supported: " + encoding);
return;
}

Uri uri = Uri.parse(fileUri);
InputStream inputStream = null;
String fileContent;

try {
// Handle both file:// and content:// URIs
if ("file".equalsIgnoreCase(uri.getScheme())) {
File file = new File(uri.getPath());
if (!file.exists()) {
Copy link
Copy Markdown
Member

@RohitKushvaha01 RohitKushvaha01 Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check file.canRead(). Sometimes invalid URIs get converted to /, which exists but is not what we expect.

Also check file.isFile() for the reasons stated above.

callback.error("File does not exist");
return;
}
inputStream = new FileInputStream(file);
} else {
inputStream = context.getContentResolver().openInputStream(uri);
}

if (inputStream == null) {
callback.error("Cannot open file");
return;
}

// Read file content with specified encoding
Charset charset = Charset.forName(encoding);
byte[] bytes = new byte[inputStream.available()];
int totalRead = 0;
int bytesRead;

// Read in chunks to handle large files
java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();
byte[] chunk = new byte[8192];
while ((bytesRead = inputStream.read(chunk)) != -1) {
Copy link
Copy Markdown
Member

@RohitKushvaha01 RohitKushvaha01 Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are better ways to read from a stream/file, this is one of the most error prone method

For java.io.File you can just do file.readText(encoding)

buffer.write(chunk, 0, bytesRead);
}
bytes = buffer.toByteArray();
Comment thread
bajrangCoder marked this conversation as resolved.
Outdated

CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(bytes));
fileContent = charBuffer.toString();

} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignored) {}
}
}

// check length first
if (fileContent.length() != currentText.length()) {
Copy link
Copy Markdown
Member

@RohitKushvaha01 RohitKushvaha01 Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this check above the stream-reading code to avoid reading the file. Also, use file.length() instead of the file content length. (Only applicable for java.io.File)

callback.success(1); // Changed
return;
}

// Full comparison
if (fileContent.equals(currentText)) {
callback.success(0); // Not changed
} else {
callback.success(1); // Changed
}

} catch (Exception e) {
callback.error(e.toString());
}
}

/**
* Compares two text strings.
* This method runs in a background thread to avoid blocking the UI
* for large string comparisons.
*
* @param text1 First text to compare
* @param text2 Second text to compare
* @param callback Returns 1 if texts are different, 0 if same
*/
private void compareTexts(
String text1,
String text2,
CallbackContext callback
) {
try {
if (text1 == null) text1 = "";
if (text2 == null) text2 = "";

// check length first
if (text1.length() != text2.length()) {
callback.success(1); // Changed
return;
}

// Full comparison
if (text1.equals(text2)) {
callback.success(0); // Not changed
} else {
callback.success(1); // Changed
}

} catch (Exception e) {
callback.error(e.toString());
}
}

private void getAvailableEncodings(CallbackContext callback) {
try {
Map < String, Charset > charsets = Charset.availableCharsets();
Expand Down
39 changes: 39 additions & 0 deletions src/plugins/system/www/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,44 @@ module.exports = {
},
getGlobalSetting: function (key, onSuccess, onFail) {
cordova.exec(onSuccess, onFail, 'System', 'get-global-setting', [key]);
},
/**
* Compare file content with provided text in a background thread.
* @param {string} fileUri - The URI of the file to read
* @param {string} encoding - The character encoding to use
* @param {string} currentText - The text to compare against
* @returns {Promise<boolean>} - Resolves to true if content differs, false if same
*/
compareFileText: function (fileUri, encoding, currentText) {
return new Promise((resolve, reject) => {
cordova.exec(
function(result) {
resolve(result === 1);
},
reject,
'System',
'compare-file-text',
[fileUri, encoding, currentText]
);
});
},
/**
* Compare two text strings in a background thread.
* @param {string} text1 - First text to compare
* @param {string} text2 - Second text to compare
* @returns {Promise<boolean>} - Resolves to true if texts differ, false if same
*/
compareTexts: function (text1, text2) {
return new Promise((resolve, reject) => {
cordova.exec(
function(result) {
resolve(result === 1);
},
reject,
'System',
'compare-texts',
[text1, text2]
);
});
}
};