Skip to content

Commit 75b2a18

Browse files
authored
perf(file-system): single-pass Android readTextSync that keeps line breaks (#11653)
* perf(file-system): read the whole file in Android readTextSync 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. * fix(file-system): return Android readTextSync content unchanged 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.
1 parent 2f5441a commit 75b2a18

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

‎apps/automated/src/file-system/file-system-tests.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,3 +785,19 @@ export function test_FileAppendText(done) {
785785
.then(() => done())
786786
.catch(done);
787787
}
788+
789+
export function test_FileReadTextKeepsLineBreaks(done) {
790+
const content = '\nfirst\r\nsecond\rthird\n\n' + 'more than one read chunk\r\n'.repeat(5000);
791+
const file = fs.File.fromPath(fs.path.join(fs.knownFolders.temp().path, `${Date.now()}-line-breaks.txt`));
792+
file.writeTextSync(content);
793+
TKUnit.assertEqual(file.readTextSync(), content, 'readTextSync should return the file content unchanged');
794+
file
795+
.readText()
796+
.then((value) => {
797+
TKUnit.assertEqual(value, content, 'readText should return the file content unchanged');
798+
799+
return Promise.allSettled([file.remove()]);
800+
})
801+
.then(() => done())
802+
.catch(done);
803+
}

‎packages/core/file-system/file-system-access.android.ts‎

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -551,42 +551,18 @@ export class FileSystemAccess implements IFileSystemAccess {
551551

552552
public readTextSync(path: string, onError?: (error: any) => any, encoding?: any) {
553553
try {
554-
const javaFile = new java.io.File(path);
555-
const stream = new java.io.FileInputStream(javaFile);
556-
557554
let actualEncoding = encoding;
558555
if (!actualEncoding) {
559556
actualEncoding = textModule.encoding.UTF_8;
560557
}
561-
const reader = new java.io.InputStreamReader(stream, actualEncoding);
562-
const bufferedReader = new java.io.BufferedReader(reader);
563-
564-
// TODO: We will need to read the entire file to a CharBuffer instead of reading it line by line
565-
// TODO: bufferedReader.read(CharBuffer) does not currently work
566-
let line = undefined;
567-
let result = '';
568-
while (line !== null) {
569-
line = bufferedReader.readLine();
570-
if (line === null) {
571-
break;
572-
}
573558

574-
if (result.length > 0) {
575-
// add the new line manually to the result
576-
// TODO: Try with CharBuffer at a later stage, when the Bridge allows it
577-
result += '\n';
578-
}
579-
580-
result += line;
581-
}
559+
let result = FileSystemAccess._readAllBytes(path).toString(actualEncoding);
582560

583561
if (actualEncoding === textModule.encoding.UTF_8) {
584562
// Remove UTF8 BOM if present. http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html
585563
result = FileSystemAccess._removeUtf8Bom(result);
586564
}
587565

588-
bufferedReader.close();
589-
590566
return result;
591567
} catch (exception) {
592568
if (onError) {
@@ -595,6 +571,24 @@ export class FileSystemAccess implements IFileSystemAccess {
595571
}
596572
}
597573

574+
private static _readAllBytes(path: string): java.io.ByteArrayOutputStream {
575+
const javaFile = new java.io.File(path);
576+
const stream = new java.io.FileInputStream(javaFile);
577+
try {
578+
// length() is only a hint: special files such as /proc entries report 0.
579+
const output = new java.io.ByteArrayOutputStream(Math.max(javaFile.length(), 32));
580+
const chunk = (<any>Array).create('byte', 65536);
581+
let count: number;
582+
while ((count = stream.read(chunk)) !== -1) {
583+
output.write(chunk, 0, count);
584+
}
585+
586+
return output;
587+
} finally {
588+
stream.close();
589+
}
590+
}
591+
598592
private static _removeUtf8Bom(s: string): string {
599593
if (s.charCodeAt(0) === 0xfeff) {
600594
s = s.slice(1);

0 commit comments

Comments
 (0)