Skip to content

Commit 49cb8a6

Browse files
committed
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.
1 parent 283eb0c commit 49cb8a6

1 file changed

Lines changed: 29 additions & 25 deletions

File tree

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

Lines changed: 29 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._readLinesLegacy(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,34 @@ 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+
592+
// Line-by-line semantics: CR/CRLF become LF, leading blank lines and the final line break are dropped.
593+
private static _readLinesLegacy(text: string): string {
594+
let result = text.replace(/\r\n?/g, '\n');
595+
if (result.endsWith('\n')) {
596+
result = result.slice(0, -1);
597+
}
598+
599+
return result.replace(/^\n+/, '');
600+
}
601+
598602
private static _removeUtf8Bom(s: string): string {
599603
if (s.charCodeAt(0) === 0xfeff) {
600604
s = s.slice(1);

0 commit comments

Comments
 (0)