diff options
| author | Harsh Shandilya <me@msfjarvis.dev> | 2021-04-30 23:22:01 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-30 23:22:01 +0530 |
| commit | f834c754e68eaea8cb32e1368f61f36eaf573aaa (patch) | |
| tree | 5a4e77697eb1a28e0bd328d019c2440063b63c62 /openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt | |
| parent | 4880e1db276b4cf2ec29eeffbd38539ca643b4be (diff) | |
| download | HardenedPass-f834c754e68eaea8cb32e1368f61f36eaf573aaa.tar.gz HardenedPass-f834c754e68eaea8cb32e1368f61f36eaf573aaa.zip | |
Refactor openpgp-ktx to leverage coroutines (#1398)
* openpgp-ktx: leverage coroutines for async IPC
* Unwind nested dispatchers
* Fix name shadowing warning
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt')
| -rw-r--r-- | openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt index dc3ef736..1229b95c 100644 --- a/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt +++ b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt @@ -2,6 +2,8 @@ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ +@file:Suppress("BlockingMethodInNonBlockingContext") + package me.msfjarvis.openpgpktx.util import android.os.ParcelFileDescriptor @@ -11,30 +13,27 @@ import android.util.Log import java.io.IOException import java.io.InputStream import java.io.OutputStream +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext internal object ParcelFileDescriptorUtil { private const val TAG = "PFDUtils" - @Throws(IOException::class) - internal fun pipeFrom(inputStream: InputStream): ParcelFileDescriptor { + internal suspend fun pipeFrom(inputStream: InputStream): ParcelFileDescriptor { val pipe = ParcelFileDescriptor.createPipe() val readSide = pipe[0] val writeSide = pipe[1] - TransferThread(inputStream, AutoCloseOutputStream(writeSide)).start() + transferStreams(inputStream, AutoCloseOutputStream(writeSide)) return readSide } - @Throws(IOException::class) - internal fun pipeTo(outputStream: OutputStream, output: ParcelFileDescriptor?): TransferThread { - val t = TransferThread(AutoCloseInputStream(output), outputStream) - t.start() - return t + internal suspend fun pipeTo(outputStream: OutputStream, output: ParcelFileDescriptor?) { + transferStreams(AutoCloseInputStream(output), outputStream) } - internal class TransferThread(val `in`: InputStream, private val out: OutputStream) : Thread("IPC Transfer Thread") { - - override fun run() { + private suspend fun transferStreams(`in`: InputStream, `out`: OutputStream) { + withContext(Dispatchers.IO) { val buf = ByteArray(4096) var len: Int try { @@ -52,9 +51,5 @@ internal object ParcelFileDescriptorUtil { } catch (ignored: IOException) {} } } - - init { - isDaemon = true - } } } |
