mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
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:
parent
25e467b3a5
commit
1d8d1d6b03
1 changed files with 5 additions and 4 deletions
|
|
@ -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")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue