Merge pull request #1996 from johnjohndoe/file-extensions

Use File extension functions to make code more concise.
This commit is contained in:
Benoit Marty 2020-08-25 18:09:00 +02:00 committed by GitHub
commit a5f537f251
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 13 additions and 22 deletions

View file

@ -20,7 +20,7 @@ Build 🧱:
- Some dependencies have been upgraded (coroutine, recyclerView, appCompat, core-ktx, firebase-messaging)
Other changes:
-
- Use File extension functions to make code more concise (#1996)
Changes in Element 1.0.5 (2020-08-21)
===================================================

View file

@ -29,7 +29,6 @@ import org.junit.FixMethodOrder
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters
import java.io.ByteArrayInputStream
import java.io.InputStream
/**
@ -46,7 +45,7 @@ class AttachmentEncryptionTest {
val inputStream: InputStream
inputStream = if (`in`.isEmpty()) {
ByteArrayInputStream(`in`)
`in`.inputStream()
} else {
val memoryFile = MemoryFile("file" + System.currentTimeMillis(), `in`.size)
memoryFile.outputStream.write(`in`)

View file

@ -21,7 +21,6 @@ import android.util.Base64
import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileInfo
import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileKey
import timber.log.Timber
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.security.MessageDigest
@ -179,7 +178,7 @@ internal object MXEncryptedAttachments {
return null
}
return ByteArrayInputStream(outputStream.toByteArray())
return outputStream.toByteArray().inputStream()
.also { Timber.v("Decrypt in ${System.currentTimeMillis() - t0}ms") }
} catch (oom: OutOfMemoryError) {
Timber.e(oom, "## decryptAttachment() failed: OOM")

View file

@ -21,7 +21,6 @@ import android.util.Base64
import io.realm.Realm
import io.realm.RealmConfiguration
import io.realm.RealmObject
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectOutputStream
import java.util.zip.GZIPInputStream
@ -96,7 +95,7 @@ fun <T> deserializeFromRealm(string: String?): T? {
}
val decodedB64 = Base64.decode(string.toByteArray(), Base64.DEFAULT)
val bais = ByteArrayInputStream(decodedB64)
val bais = decodedB64.inputStream()
val gzis = GZIPInputStream(bais)
val ois = SafeObjectInputStream(gzis)
return ois.use {

View file

@ -42,10 +42,7 @@ import org.matrix.android.sdk.internal.worker.SessionWorkerParams
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
import org.matrix.android.sdk.internal.worker.getSessionComponent
import timber.log.Timber
import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.UUID
import javax.inject.Inject
@ -130,7 +127,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
val contentUploadResponse = if (params.isEncrypted) {
Timber.v("Encrypt thumbnail")
notifyTracker(params) { contentUploadStateTracker.setEncryptingThumbnail(it) }
val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType)
val encryptionResult = MXEncryptedAttachments.encryptAttachment(thumbnailData.bytes.inputStream(), thumbnailData.mimeType)
uploadedThumbnailEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader.uploadByteArray(encryptionResult.encryptedByteArray,
"thumb_${attachment.name}",
@ -176,7 +173,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
cacheFile.createNewFile()
cacheFile.deleteOnExit()
val outputStream = FileOutputStream(cacheFile)
val outputStream = cacheFile.outputStream()
outputStream.use {
inputStream.copyTo(outputStream)
}
@ -203,7 +200,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
Timber.v("Encrypt file")
notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(cacheFile), attachment.getSafeMimeType())
val encryptionResult = MXEncryptedAttachments.encryptAttachment(cacheFile.inputStream(), attachment.getSafeMimeType())
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader

View file

@ -219,7 +219,7 @@ internal class SecretStoringUtils @Inject constructor(private val context: Conte
@RequiresApi(Build.VERSION_CODES.M)
private fun decryptStringM(encryptedChunk: ByteArray, keyAlias: String): String {
val (iv, encryptedText) = formatMExtract(ByteArrayInputStream(encryptedChunk))
val (iv, encryptedText) = formatMExtract(encryptedChunk.inputStream())
val secretKey = getOrGenerateSymmetricKeyForAliasM(keyAlias)

View file

@ -19,7 +19,6 @@ package org.matrix.android.sdk.internal.util
import androidx.annotation.WorkerThread
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
/**
@ -27,7 +26,7 @@ import java.io.InputStream
*/
@WorkerThread
fun writeToFile(inputStream: InputStream, outputFile: File) {
FileOutputStream(outputFile).use {
outputFile.outputStream().use {
inputStream.copyTo(it)
}
}

View file

@ -31,7 +31,6 @@ import okhttp3.OkHttpClient
import okhttp3.Request
import timber.log.Timber
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
@ -97,7 +96,7 @@ class VectorGlideDataFetcher(private val activeSessionHolder: ActiveSessionHolde
Timber.v("Load data: $data")
if (data.isLocalFile() && data.url != null) {
val initialFile = File(data.url)
callback.onDataReady(FileInputStream(initialFile))
callback.onDataReady(initialFile.inputStream())
return
}
val contentUrlResolver = activeSessionHolder.getActiveSession().contentUrlResolver()

View file

@ -518,8 +518,8 @@ fun saveFileIntoLegacy(sourceFile: File, dstDirPath: File, outputFilename: Strin
var outputStream: FileOutputStream? = null
try {
dstFile.createNewFile()
inputStream = FileInputStream(sourceFile)
outputStream = FileOutputStream(dstFile)
inputStream = sourceFile.inputStream()
outputStream = dstFile.outputStream()
val buffer = ByteArray(1024 * 10)
var len: Int
while (inputStream.read(buffer).also { len = it } != -1) {

View file

@ -32,7 +32,6 @@ import org.matrix.android.sdk.api.session.content.ContentUrlResolver
import me.gujun.android.span.span
import timber.log.Timber
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import javax.inject.Inject
import javax.inject.Singleton
@ -494,7 +493,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
try {
val file = File(context.applicationContext.cacheDir, ROOMS_NOTIFICATIONS_FILE_NAME)
if (file.exists()) {
FileInputStream(file).use {
file.inputStream().use {
val events: ArrayList<NotifiableEvent>? = currentSession?.loadSecureSecret(it, KEY_ALIAS_SECRET_STORAGE)
if (events != null) {
return events.toMutableList()