From 34a78e635e015932cf84dc557e70d8b48c2c08be Mon Sep 17 00:00:00 2001 From: ShellWen Chen Date: Mon, 15 Jul 2024 07:30:08 +0800 Subject: [PATCH] pref: reduce reflection calls --- .../SftpPlugin/saf/SafFileSystemProvider.kt | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/org/kde/kdeconnect/Plugins/SftpPlugin/saf/SafFileSystemProvider.kt b/src/org/kde/kdeconnect/Plugins/SftpPlugin/saf/SafFileSystemProvider.kt index 906f9095..453e5fa2 100644 --- a/src/org/kde/kdeconnect/Plugins/SftpPlugin/saf/SafFileSystemProvider.kt +++ b/src/org/kde/kdeconnect/Plugins/SftpPlugin/saf/SafFileSystemProvider.kt @@ -14,6 +14,7 @@ import android.provider.DocumentsContract import android.util.Log import java.io.FileNotFoundException import java.io.IOException +import java.lang.reflect.Method import java.net.URI import java.nio.channels.FileChannel import java.nio.channels.SeekableByteChannel @@ -74,12 +75,10 @@ class SafFileSystemProvider( val channel = newFileChannel(path, options, *attrs_) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { - val clazz = Class.forName("j$.nio.channels.DesugarChannels") - val method = clazz.getDeclaredMethod( - "convertMaybeLegacyFileChannelFromLibrary", - FileChannel::class.java - ) - return method.invoke(null, channel) as SeekableByteChannel + return convertMaybeLegacyFileChannelFromLibraryFunction.invoke( + null, + channel + ) as SeekableByteChannel } return channel @@ -137,7 +136,10 @@ class SafFileSystemProvider( StandardOpenOption.CREATE_NEW, ) -> { val docFile = path.getDocumentFile(context) ?: run { - if (options.contains(StandardOpenOption.CREATE) || options.contains(StandardOpenOption.CREATE_NEW)) { + if (options.contains(StandardOpenOption.CREATE) || options.contains( + StandardOpenOption.CREATE_NEW + ) + ) { val parent = path.parent.getDocumentFile(context)!! parent.createFile(Files.probeContentType(path), path.names.last()).apply { if (this == null) { @@ -600,5 +602,13 @@ class SafFileSystemProvider( companion object { private const val TAG = "SafFileSystemProvider" + + private val convertMaybeLegacyFileChannelFromLibraryFunction: Method by lazy { + val clazz = Class.forName("j$.nio.channels.DesugarChannels") + clazz.getDeclaredMethod( + "convertMaybeLegacyFileChannelFromLibrary", + FileChannel::class.java + ) + } } } \ No newline at end of file