Skip to content

Commit 1d8d1d6

Browse files
pshnicolas-raoul
andauthored
Remove the extra byte buffer copying while creating file chunks (commons-app#6091)
Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
1 parent 25e467b commit 1d8d1d6

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ class FileUtilsWrapper @Inject constructor(private val context: Context) {
4949
while ((bis.read(buffer).also { size = it }) > 0) {
5050
buffers.add(
5151
writeToFile(
52-
buffer.copyOf(size),
52+
buffer,
5353
file.name ?: "",
54-
getFileExt(file.name)
54+
getFileExt(file.name),
55+
size
5556
)
5657
)
5758
}
@@ -67,15 +68,15 @@ class FileUtilsWrapper @Inject constructor(private val context: Context) {
6768
* Create a temp file containing the passed byte data.
6869
*/
6970
@Throws(IOException::class)
70-
private fun writeToFile(data: ByteArray, fileName: String, fileExtension: String): File {
71+
private fun writeToFile(data: ByteArray, fileName: String, fileExtension: String, size: Int): File {
7172
val file = File.createTempFile(fileName, fileExtension, context.cacheDir)
7273
try {
7374
if (!file.exists()) {
7475
file.createNewFile()
7576
}
7677

7778
FileOutputStream(file).use { fos ->
78-
fos.write(data)
79+
fos.write(data, 0, size)
7980
}
8081
} catch (throwable: Exception) {
8182
Timber.e(throwable, "Failed to create file")

0 commit comments

Comments
 (0)