aboutsummaryrefslogtreecommitdiffstats
path: root/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-11-25 03:27:42 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-11-29 17:51:16 +0530
commit7b25bf878315a894122e3de74e19add6fcdf5d4c (patch)
treeccb89e08265d628f8ff9a3de562ddb4252199dee /openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt
parentfa2a93769f5cec8397064337fd53659147ba1877 (diff)
downloadHardenedPass-7b25bf878315a894122e3de74e19add6fcdf5d4c.tar.gz
HardenedPass-7b25bf878315a894122e3de74e19add6fcdf5d4c.zip
openpgp-ktx: import at 95eed95fb5654e9dcfe32c3712ae97385e186396
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.kt63
1 files changed, 63 insertions, 0 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
new file mode 100644
index 00000000..6f1f9eda
--- /dev/null
+++ b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/ParcelFileDescriptorUtil.kt
@@ -0,0 +1,63 @@
+/*
+ * Copyright © 2019 The Android Password Authors. All Rights Reserved.
+ * SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception
+ */
+package me.msfjarvis.openpgpktx.util
+
+import android.os.ParcelFileDescriptor
+import android.os.ParcelFileDescriptor.AutoCloseInputStream
+import android.os.ParcelFileDescriptor.AutoCloseOutputStream
+import android.util.Log
+import java.io.IOException
+import java.io.InputStream
+import java.io.OutputStream
+
+internal object ParcelFileDescriptorUtil {
+
+ private const val TAG = "PFDUtils"
+
+ @Throws(IOException::class)
+ internal fun pipeFrom(inputStream: InputStream): ParcelFileDescriptor {
+ val pipe = ParcelFileDescriptor.createPipe()
+ val readSide = pipe[0]
+ val writeSide = pipe[1]
+ TransferThread(inputStream, AutoCloseOutputStream(writeSide))
+ .start()
+ return readSide
+ }
+
+ @Throws(IOException::class)
+ internal fun pipeTo(outputStream: OutputStream, output: ParcelFileDescriptor?): TransferThread {
+ val t = TransferThread(AutoCloseInputStream(output), outputStream)
+ t.start()
+ return t
+ }
+
+ internal class TransferThread(val `in`: InputStream, private val out: OutputStream) : Thread("IPC Transfer Thread") {
+
+ override fun run() {
+ val buf = ByteArray(4096)
+ var len: Int
+ try {
+ while (`in`.read(buf).also { len = it } > 0) {
+ out.write(buf, 0, len)
+ }
+ } catch (e: IOException) {
+ Log.e(TAG, "IOException when writing to out", e)
+ } finally {
+ try {
+ `in`.close()
+ } catch (ignored: IOException) {
+ }
+ try {
+ out.close()
+ } catch (ignored: IOException) {
+ }
+ }
+ }
+
+ init {
+ isDaemon = true
+ }
+ }
+}