Просмотр исходного кода

move streams to utilize "use"

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 2 лет назад
Родитель
Сommit
5a2b04740b

+ 7 - 10
app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt

@@ -1543,19 +1543,16 @@ class ChatController(args: Bundle) :
         val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey)
         val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey)
 
 
         val fd: AssetFileDescriptor = activity?.contentResolver!!.openAssetFileDescriptor(uri, "r")!!
         val fd: AssetFileDescriptor = activity?.contentResolver!!.openAssetFileDescriptor(uri, "r")!!
-        val fis = fd.createInputStream()
+        fd.use {
+            val fis = fd.createInputStream()
 
 
-        file.createNewFile()
-        fis.use { input ->
-            file.outputStream().use { output ->
-                input.copyTo(output)
+            file.createNewFile()
+            fis.use { input ->
+                file.outputStream().use { output ->
+                    input.copyTo(output)
+                }
             }
             }
         }
         }
-        try {
-            fd.close()
-        } catch (e: IOException) {
-            Log.w(TAG, "Failed to close AssetFileDescriptor", e)
-        }
     }
     }
 
 
     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {

+ 5 - 4
app/src/main/java/com/nextcloud/talk/upload/normal/FileUploader.kt

@@ -54,11 +54,12 @@ class FileUploader(
         var requestBody: RequestBody? = null
         var requestBody: RequestBody? = null
         try {
         try {
             val input: InputStream = context.contentResolver.openInputStream(sourceFileUri)!!
             val input: InputStream = context.contentResolver.openInputStream(sourceFileUri)!!
-            val buf = ByteArray(input.available())
-            while (input.read(buf) != -1) {
-                requestBody = RequestBody.create("application/octet-stream".toMediaTypeOrNull(), buf)
+            input.use {
+                val buf = ByteArray(input.available())
+                while (it.read(buf) != -1) {
+                    requestBody = RequestBody.create("application/octet-stream".toMediaTypeOrNull(), buf)
+                }
             }
             }
-            input.close()
         } catch (e: Exception) {
         } catch (e: Exception) {
             Log.e(TAG, "failed to create RequestBody for $sourceFileUri", e)
             Log.e(TAG, "failed to create RequestBody for $sourceFileUri", e)
         }
         }