Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions apps/automated/src/file-system/file-system-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,19 @@ export function test_FileAppendText(done) {
.then(() => done())
.catch(done);
}

export function test_FileReadTextKeepsLineBreaks(done) {
const content = '\nfirst\r\nsecond\rthird\n\n' + 'more than one read chunk\r\n'.repeat(5000);
const file = fs.File.fromPath(fs.path.join(fs.knownFolders.temp().path, `${Date.now()}-line-breaks.txt`));
file.writeTextSync(content);
TKUnit.assertEqual(file.readTextSync(), content, 'readTextSync should return the file content unchanged');
file
.readText()
.then((value) => {
TKUnit.assertEqual(value, content, 'readText should return the file content unchanged');

return Promise.allSettled([file.remove()]);
})
.then(() => done())
.catch(done);
}
44 changes: 19 additions & 25 deletions packages/core/file-system/file-system-access.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,42 +551,18 @@ export class FileSystemAccess implements IFileSystemAccess {

public readTextSync(path: string, onError?: (error: any) => any, encoding?: any) {
try {
const javaFile = new java.io.File(path);
const stream = new java.io.FileInputStream(javaFile);

let actualEncoding = encoding;
if (!actualEncoding) {
actualEncoding = textModule.encoding.UTF_8;
}
const reader = new java.io.InputStreamReader(stream, actualEncoding);
const bufferedReader = new java.io.BufferedReader(reader);

// TODO: We will need to read the entire file to a CharBuffer instead of reading it line by line
// TODO: bufferedReader.read(CharBuffer) does not currently work
let line = undefined;
let result = '';
while (line !== null) {
line = bufferedReader.readLine();
if (line === null) {
break;
}

if (result.length > 0) {
// add the new line manually to the result
// TODO: Try with CharBuffer at a later stage, when the Bridge allows it
result += '\n';
}

result += line;
}
let result = FileSystemAccess._readAllBytes(path).toString(actualEncoding);

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

bufferedReader.close();

return result;
} catch (exception) {
if (onError) {
Expand All @@ -595,6 +571,24 @@ export class FileSystemAccess implements IFileSystemAccess {
}
}

private static _readAllBytes(path: string): java.io.ByteArrayOutputStream {
const javaFile = new java.io.File(path);
const stream = new java.io.FileInputStream(javaFile);
try {
// length() is only a hint: special files such as /proc entries report 0.
const output = new java.io.ByteArrayOutputStream(Math.max(javaFile.length(), 32));
const chunk = (<any>Array).create('byte', 65536);
let count: number;
while ((count = stream.read(chunk)) !== -1) {
output.write(chunk, 0, count);
}

return output;
} finally {
stream.close();
}
}

private static _removeUtf8Bom(s: string): string {
if (s.charCodeAt(0) === 0xfeff) {
s = s.slice(1);
Expand Down
Loading