Here is a reproducible code: https://gist.github.com/somidad/1c347b650039a1a140baf6f232683bbe
Based on the result, what I understand is that:
- If the length is multiple of 4, writing succeeds and reading correctly recovers the original string
- Else if the length modulo 4 is equal to 1, writing raises an error
- Else if the length modulo 4 is equal to 2 or 3, writing and reading did work, but the last character gets corrupted (and padding follows)
Is it expected behavior? Or am I missing something to get it to work as expected?
For those who may not want to visit my gist, I copied and pasted it below:
Code
import {
documentDirectory,
readAsStringAsync,
writeAsStringAsync,
} from "expo-file-system";
import { Button, View } from "react-native";
export default function Temp() {
async function temp(s: string) {
try {
await writeAsStringAsync(documentDirectory + "temp.txt", s, {});
await writeAsStringAsync(documentDirectory + "temp.bin", s, {
encoding: "base64",
});
const t2t = await readAsStringAsync(documentDirectory + "temp.txt", {});
const b2b = await readAsStringAsync(documentDirectory + "temp.bin", {
encoding: "base64",
});
console.log("Original".padEnd(48, " "), s);
console.log(" Write as text, read as text:".padEnd(48, " "), t2t);
console.log(" Write as base64, read as base64:".padEnd(48, " "), b2b);
} catch (error) {
console.error(error);
}
}
return (
<View style={{ padding: 8 }}>
<Button
title="Temp"
onPress={async () => {
console.log("Test");
const s = "0123456789abcdef";
for (let i = 1; i <= s.length; i++) {
await temp(s.substring(0, i));
}
console.log("========");
}}
/>
</View>
);
}
Result (log)
(NOBRIDGE) LOG Test
(NOBRIDGE) ERROR [Error: Call to function 'ExponentFileSystem.writeAsStringAsync' has been rejected.
→ Caused by: java.lang.IllegalArgumentException: bad base-64]
(NOBRIDGE) LOG Original 01
(NOBRIDGE) LOG Write as text, read as text: 01
(NOBRIDGE) LOG Write as base64, read as base64: 0w==
(NOBRIDGE) LOG Original 012
(NOBRIDGE) LOG Write as text, read as text: 012
(NOBRIDGE) LOG Write as base64, read as base64: 010=
(NOBRIDGE) LOG Original 0123
(NOBRIDGE) LOG Write as text, read as text: 0123
(NOBRIDGE) LOG Write as base64, read as base64: 0123
(Omitted logs showing the same pattern)