Repository navigation
perf(file-system): single-pass Android readTextSync that keeps line breaks - #11653
Merged
Merged
Conversation
readTextSync made one JNI readLine() call per line and concatenated the lines in JS. It now reads the bytes in 64 KiB chunks and decodes them with a single ByteArrayOutputStream.toString(charset) call, and always closes the stream. The line-by-line output is kept for now: CR and CRLF become LF, leading blank lines and the final line break are dropped.
readTextSync rebuilt the text from readLine() results, so CR and CRLF became LF, leading blank lines and the final line break were dropped. It now returns the decoded file content as is, like readText() on Android and readTextSync()/readText() on iOS. The UTF-8 BOM is still removed.
|
View your CI Pipeline Execution ↗ for commit 2a321da
💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗ ☁️ Nx Cloud last updated this comment at |
4 of 5 tasks
commit: |
NathanWalker
approved these changes
Nov 21, 2026
NathanWalker
left a comment
Contributor
There was a problem hiding this comment.
Reading in 64 KiB chunks into ByteArrayOutputStream with try/finally stream cleanup and Math.max(javaFile.length(), 32) as a size hint (keeping zero-length special files like /proc/* working) is a solid win over per-line JNI readLine() calls, and preserving original line breaks brings Android readTextSync in line with Android readText() and iOS. Appreciate splitting the perf and behavior commits as well.
farfromrefug
approved these changes
Nov 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
What is the current behavior?
Android
readTextSync(file-system/file-system-access.android.ts) makes one JNIreadLine()call per line and rebuilds the text from the lines. That is slow, and it also changes the content:\r\nand lone\rbecome\n;Android
readText()(Async.File.readText) and iOSreadTextSync()/readText()return the file unchanged, so the same file reads differently depending on the API and platform.What is the new behavior?
Two commits, so either can be reverted alone.
perf(file-system): read the whole filereadTextSyncreads the file in 64 KiB chunks into aByteArrayOutputStreamand decodes it with onetoString(charset)call. This commit keeps the old output exactly, through a small_readLinesLegacystep.java.nio.file.Filesisn't used: it needs API 26, and the widgets minSdk is 21.File.length()is only a size hint, so special files like/proc/*(which report a length of 0) still read correctly.encodingparameter, the UTF-8 BOM removal and theonErrorbehaviour are unchanged.fix(file-system): return the content unchangedImportant
Behaviour change.
readTextSyncon Android now returns the file content unchanged, like AndroidreadText()and both iOS functions. Android-only code that compares a file's contents exactly will now see the trailing line break, and code that splits on\nkeeps the\rof CRLF files. The UTF-8 BOM is still removed.test_FileReadTextKeepsLineBreaksinapps/automatedcovers this. It writes CRLF, CR and LF content larger than one read chunk and checks thatreadTextSync()andreadText()both return it unchanged.Verification
apps/automatedon an Android emulator (API 36,@nativescript/android9.1.1) passes.readTextSync()of a 1 MiB file with 16,645 CRLF lines went from ~12 ms to 2–3.5 ms. It also checked that the BOM is still stripped,onErroris still called withundefinedreturned for a missing file, and/proc/self/statusstill reads.Split out of #11636.