Remove the extra byte buffer copying while creating file chunks (#6091)

Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
This commit is contained in:
Paul Hawke 2025-01-05 05:44:30 -06:00 committed by GitHub
parent 25e467b3a5
commit 1d8d1d6b03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -49,9 +49,10 @@ class FileUtilsWrapper @Inject constructor(private val context: Context) {
while ((bis.read(buffer).also { size = it }) > 0) { while ((bis.read(buffer).also { size = it }) > 0) {
buffers.add( buffers.add(
writeToFile( writeToFile(
buffer.copyOf(size), buffer,
file.name ?: "", 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. * Create a temp file containing the passed byte data.
*/ */
@Throws(IOException::class) @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) val file = File.createTempFile(fileName, fileExtension, context.cacheDir)
try { try {
if (!file.exists()) { if (!file.exists()) {
@ -75,7 +76,7 @@ class FileUtilsWrapper @Inject constructor(private val context: Context) {
} }
FileOutputStream(file).use { fos -> FileOutputStream(file).use { fos ->
fos.write(data) fos.write(data, 0, size)
} }
} catch (throwable: Exception) { } catch (throwable: Exception) {
Timber.e(throwable, "Failed to create file") Timber.e(throwable, "Failed to create file")