From 1d8d1d6b032aa165e581f2ce4c3e683ec820270a Mon Sep 17 00:00:00 2001 From: Paul Hawke Date: Sun, 5 Jan 2025 05:44:30 -0600 Subject: [PATCH] Remove the extra byte buffer copying while creating file chunks (#6091) Co-authored-by: Nicolas Raoul --- .../java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt b/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt index aa1f8aed6..68a3cb362 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt @@ -49,9 +49,10 @@ class FileUtilsWrapper @Inject constructor(private val context: Context) { while ((bis.read(buffer).also { size = it }) > 0) { buffers.add( writeToFile( - buffer.copyOf(size), + buffer, file.name ?: "", - getFileExt(file.name) + getFileExt(file.name), + size ) ) } @@ -67,7 +68,7 @@ class FileUtilsWrapper @Inject constructor(private val context: Context) { * Create a temp file containing the passed byte data. */ @Throws(IOException::class) - private fun writeToFile(data: ByteArray, fileName: String, fileExtension: String): File { + private fun writeToFile(data: ByteArray, fileName: String, fileExtension: String, size: Int): File { val file = File.createTempFile(fileName, fileExtension, context.cacheDir) try { if (!file.exists()) { @@ -75,7 +76,7 @@ class FileUtilsWrapper @Inject constructor(private val context: Context) { } FileOutputStream(file).use { fos -> - fos.write(data) + fos.write(data, 0, size) } } catch (throwable: Exception) { Timber.e(throwable, "Failed to create file")