Skip to content

Commit aff3b32

Browse files
committed
fix(saf): duplicating a large file no longer crashes the app (#1023)
Copying read the whole file into memory first, so duplicating a 118 MB attachment ran a 192 MB heap out and took the app down. It streams now, in 64 KB pieces. On an emulator the same file copies in 0.5 s with an identical sha256, alone and inside a folder duplicate.
1 parent e38ea34 commit aff3b32

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎android/app/src/main/java/md/zennotes/SafFsPlugin.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,6 @@ private void copyRec(Uri tree, String from, String to) throws Exception {
543543
}
544544
return;
545545
}
546-
byte[] bytes = readAll(docUri(tree, src.docId));
547546
Entry existing = resolve(tree, to);
548547
Uri target;
549548
if (existing != null && !existing.isDir) {
@@ -557,9 +556,15 @@ private void copyRec(Uri tree, String from, String to) throws Exception {
557556
if (created == null) throw new Exception("Could not create: " + to);
558557
target = created;
559558
}
560-
try (OutputStream out = resolver().openOutputStream(target, "rwt")) {
559+
// Streamed: holding a large attachment in memory to copy it ran the
560+
// heap out and took the app down (118 MB on a 192 MB heap, #1023).
561+
try (InputStream in = resolver().openInputStream(docUri(tree, src.docId));
562+
OutputStream out = resolver().openOutputStream(target, "rwt")) {
563+
if (in == null) throw new FileNotFoundException(from);
561564
if (out == null) throw new Exception("Could not open for writing: " + to);
562-
out.write(bytes);
565+
byte[] chunk = new byte[65536];
566+
int n;
567+
while ((n = in.read(chunk)) != -1) out.write(chunk, 0, n);
563568
}
564569
invalidateParent(tree, to);
565570
}

0 commit comments

Comments
 (0)