From 1e5dcb0e24defb150c1714b9ac603364814a459a Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 15 Dec 2019 17:28:17 +0800 Subject: [PATCH] Replace all transferTo to Kotlinx transfer, downgrade JDK requirement --- .../net.mamoe.mirai/utils/io/InputUtils.kt | 2 ++ .../net/mamoe/mirai/network/BotSessionJvm.kt | 12 +++++++++++- .../net/mamoe/mirai/utils/ExternalImageJvm.kt | 17 ++++++++++------- .../net/mamoe/mirai/utils/PlatformUtilsJvm.kt | 8 ++++++-- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt index 49a78aa06..70e8e41db 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt @@ -2,6 +2,8 @@ package net.mamoe.mirai.utils.io +import kotlinx.io.InputStream +import kotlinx.io.OutputStream import kotlinx.io.core.* import kotlinx.io.pool.useInstance import kotlin.jvm.JvmName diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/BotSessionJvm.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/BotSessionJvm.kt index 3fec90adc..bee7e9a5d 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/BotSessionJvm.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/BotSessionJvm.kt @@ -1,8 +1,12 @@ package net.mamoe.mirai.network +import io.ktor.util.KtorExperimentalAPI import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.withContext +import kotlinx.io.core.copyTo import kotlinx.io.core.use +import kotlinx.io.streams.asInput +import kotlinx.io.streams.asOutput import kotlinx.io.streams.inputStream import net.mamoe.mirai.Bot import net.mamoe.mirai.message.Image @@ -31,5 +35,11 @@ actual class BotSession internal actual constructor( suspend inline fun Image.downloadAsExternalImage(): ExternalImage = download().use { it.toExternalImage() } suspend inline fun Image.downloadTo(file: File) = file.outputStream().use { downloadTo(it) } - suspend inline fun Image.downloadTo(output: OutputStream) = download().inputStream().use { input -> withContext(IO) { input.transferTo(output) } } + + /** + * 需要调用者自行 close [output] + */ + @UseExperimental(KtorExperimentalAPI::class) + suspend inline fun Image.downloadTo(output: OutputStream) = + download().inputStream().asInput().use { input -> withContext(IO) { input.copyTo(output.asOutput()) } } } \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt index a8b56eb16..c6a5d48bc 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt @@ -4,12 +4,15 @@ package net.mamoe.mirai.utils import io.ktor.util.asStream import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.io.jvm.javaio.copyTo import kotlinx.coroutines.withContext import kotlinx.io.core.Input import kotlinx.io.core.IoBuffer import kotlinx.io.core.buildPacket +import kotlinx.io.core.copyTo import kotlinx.io.errors.IOException import kotlinx.io.streams.asInput +import kotlinx.io.streams.asOutput import java.awt.image.BufferedImage import java.io.File import java.io.InputStream @@ -81,9 +84,9 @@ suspend fun File.suspendToExternalImage(): ExternalImage = withContext(IO) { toE @Throws(IOException::class) fun URL.toExternalImage(): ExternalImage { val file = createTempFile().apply { deleteOnExit() } - file.outputStream().use { output -> - openStream().use { input -> - input.transferTo(output) + file.outputStream().asOutput().use { output -> + openStream().asInput().use { input -> + input.copyTo(output) } } return file.toExternalImage() @@ -101,8 +104,8 @@ suspend fun URL.suspendToExternalImage(): ExternalImage = withContext(IO) { toEx @Throws(IOException::class) fun InputStream.toExternalImage(): ExternalImage { val file = createTempFile().apply { deleteOnExit() } - file.outputStream().use { - this.transferTo(it) + file.outputStream().asOutput().use { + this.asInput().copyTo(it) } this.close() return file.toExternalImage() @@ -120,8 +123,8 @@ suspend fun InputStream.suspendToExternalImage(): ExternalImage = withContext(IO @Throws(IOException::class) fun Input.toExternalImage(): ExternalImage { val file = createTempFile().apply { deleteOnExit() } - file.outputStream().use { - this.asStream().transferTo(it) + file.outputStream().asOutput().use { + this.asStream().asInput().copyTo(it) } return file.toExternalImage() } diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/PlatformUtilsJvm.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/PlatformUtilsJvm.kt index 1ca240a00..c1aef617b 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/PlatformUtilsJvm.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/PlatformUtilsJvm.kt @@ -4,6 +4,10 @@ package net.mamoe.mirai.utils import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO +import kotlinx.io.core.Output +import kotlinx.io.core.copyTo +import kotlinx.io.streams.asInput +import kotlinx.io.streams.asOutput import java.io.DataInput import java.io.EOFException import java.io.InputStream @@ -21,13 +25,13 @@ actual fun md5(byteArray: ByteArray): ByteArray = MessageDigest.getInstance("MD5 fun InputStream.md5(): ByteArray { val digest = MessageDigest.getInstance("md5") digest.reset() - this.transferTo(object : OutputStream() { + this.asInput().copyTo(object : OutputStream() { override fun write(b: Int) { b.toByte().let { digest.update(it) } } - }) + }.asOutput()) return digest.digest() }