move streams to utilize "use"

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-09-16 14:48:23 +02:00
parent 58436286c7
commit 5a2b04740b
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
2 changed files with 12 additions and 14 deletions

View File

@ -1543,6 +1543,7 @@ 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")!!
fd.use {
val fis = fd.createInputStream() val fis = fd.createInputStream()
file.createNewFile() file.createNewFile()
@ -1551,10 +1552,6 @@ class ChatController(args: Bundle) :
input.copyTo(output) input.copyTo(output)
} }
} }
try {
fd.close()
} catch (e: IOException) {
Log.w(TAG, "Failed to close AssetFileDescriptor", e)
} }
} }

View File

@ -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)!!
input.use {
val buf = ByteArray(input.available()) val buf = ByteArray(input.available())
while (input.read(buf) != -1) { while (it.read(buf) != -1) {
requestBody = RequestBody.create("application/octet-stream".toMediaTypeOrNull(), buf) 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)
} }