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,19 +1543,16 @@ class ChatController(args: Bundle) :
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey)
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) {

View File

@ -54,11 +54,12 @@ class FileUploader(
var requestBody: RequestBody? = null
try {
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) {
Log.e(TAG, "failed to create RequestBody for $sourceFileUri", e)
}