From 68bd83503816abb96b0b12da78111d8629bb6019 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Fri, 5 Apr 2024 13:42:02 +0800 Subject: [PATCH] Fallback to `transferTo` if `sendfile` failed --- .../kotlin/com/hippo/ehviewer/util/FDUtils.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/FDUtils.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/FDUtils.kt index 537ef05f0..ccf745fd8 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/FDUtils.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/FDUtils.kt @@ -4,18 +4,23 @@ import android.os.ParcelFileDescriptor import android.system.Int64Ref import android.system.Os import com.hippo.unifile.UniFile +import eu.kanade.tachiyomi.util.system.logcat import java.io.FileDescriptor import java.io.FileInputStream import java.io.FileOutputStream -private fun sendFileTotally(from: FileDescriptor, to: FileDescriptor) { +private fun sendFileTotally(from: FileDescriptor, to: FileDescriptor): Long { if (isAtLeastP) { - Os.sendfile(to, from, Int64Ref(0), Long.MAX_VALUE) - } else { - FileInputStream(from).use { src -> - FileOutputStream(to).use { dst -> - src.channel.transferTo(0, Long.MAX_VALUE, dst.channel) - } + // sendfile may fail on some devices + try { + return Os.sendfile(to, from, Int64Ref(0), Long.MAX_VALUE) + } catch (e: Exception) { + logcat("sendfile", e) + } + } + return FileInputStream(from).use { src -> + FileOutputStream(to).use { dst -> + src.channel.transferTo(0, Long.MAX_VALUE, dst.channel) } } }