From 582f7ef56d60b9f0119f21a0578053f758f15f89 Mon Sep 17 00:00:00 2001 From: Nelson Lopes Silva <5671236+nflsilva@users.noreply.github.com> Date: Fri, 7 Jan 2022 15:44:28 +0000 Subject: [PATCH 1/3] fix: changed writeToFileAtUrl to write files as blocks. --- src/android/LocalFilesystem.java | 43 +++++++++++++++++++------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java index 051f99496..32fce06c4 100644 --- a/src/android/LocalFilesystem.java +++ b/src/android/LocalFilesystem.java @@ -382,26 +382,35 @@ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, append = true; } - byte[] rawData; - if (isBinary) { - rawData = Base64.decode(data, Base64.DEFAULT); - } else { - rawData = data.getBytes(Charset.defaultCharset()); - } - ByteArrayInputStream in = new ByteArrayInputStream(rawData); + int totalSize = 0; try { - byte buff[] = new byte[rawData.length]; String absolutePath = filesystemPathForURL(inputURL); - FileOutputStream out = new FileOutputStream(absolutePath, append); - try { - in.read(buff, 0, buff.length); - out.write(buff, 0, rawData.length); - out.flush(); - } finally { - // Always close the output - out.close(); + //We are writting the file in 300KB blocks so we dont run out of memory. + try (FileOutputStream fileOut = new FileOutputStream(absolutePath, append)) { + byte[] byteBuffer; + int blockSize = 300 * 1024; + String block = ""; + int blockOffset = 0; + while (blockOffset < data.length()) { + + block = data.substring(blockOffset, min(blockOffset + blockSize, data.length())); + + if (isBinary) { + byteBuffer = Base64.decode(block, Base64.DEFAULT); + } else { + byteBuffer = block.getBytes(Charset.defaultCharset()); + } + + fileOut.write(byteBuffer); + fileOut.flush(); + + totalSize += byteBuffer.length; + blockOffset += blockSize; + } } + + // Always close the output if (isPublicDirectory(absolutePath)) { broadcastNewFile(Uri.fromFile(new File(absolutePath))); } @@ -414,7 +423,7 @@ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, throw realException; } - return rawData.length; + return totalSize; } private boolean isPublicDirectory(String absolutePath) { From b3150fb7610bac2deee4bdf1dd8400d04c82201a Mon Sep 17 00:00:00 2001 From: Nelson Lopes Silva <5671236+nflsilva@users.noreply.github.com> Date: Fri, 7 Jan 2022 16:30:30 +0000 Subject: [PATCH 2/3] Added missing import --- src/android/LocalFilesystem.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java index 32fce06c4..634029778 100644 --- a/src/android/LocalFilesystem.java +++ b/src/android/LocalFilesystem.java @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one */ package org.apache.cordova.file; +import static java.lang.Math.min; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; From dff244d2572f981ac16d939e9f1370ef86229826 Mon Sep 17 00:00:00 2001 From: Nelson Lopes Silva <5671236+nflsilva@users.noreply.github.com> Date: Fri, 7 Jan 2022 17:21:17 +0000 Subject: [PATCH 3/3] refactor: removed unnecessary comment --- src/android/LocalFilesystem.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java index 634029778..e2d280598 100644 --- a/src/android/LocalFilesystem.java +++ b/src/android/LocalFilesystem.java @@ -410,8 +410,7 @@ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, blockOffset += blockSize; } } - - // Always close the output + if (isPublicDirectory(absolutePath)) { broadcastNewFile(Uri.fromFile(new File(absolutePath))); }