Replace all transferTo to Kotlinx transfer, downgrade JDK requirement

This commit is contained in:
Him188 2019-12-15 17:28:17 +08:00
parent a2e82e7501
commit 1e5dcb0e24
4 changed files with 29 additions and 10 deletions

View File

@ -2,6 +2,8 @@
package net.mamoe.mirai.utils.io package net.mamoe.mirai.utils.io
import kotlinx.io.InputStream
import kotlinx.io.OutputStream
import kotlinx.io.core.* import kotlinx.io.core.*
import kotlinx.io.pool.useInstance import kotlinx.io.pool.useInstance
import kotlin.jvm.JvmName import kotlin.jvm.JvmName

View File

@ -1,8 +1,12 @@
package net.mamoe.mirai.network package net.mamoe.mirai.network
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.io.core.copyTo
import kotlinx.io.core.use import kotlinx.io.core.use
import kotlinx.io.streams.asInput
import kotlinx.io.streams.asOutput
import kotlinx.io.streams.inputStream import kotlinx.io.streams.inputStream
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.message.Image 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.downloadAsExternalImage(): ExternalImage = download().use { it.toExternalImage() }
suspend inline fun Image.downloadTo(file: File) = file.outputStream().use { downloadTo(it) } 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()) } }
} }

View File

@ -4,12 +4,15 @@ package net.mamoe.mirai.utils
import io.ktor.util.asStream import io.ktor.util.asStream
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.io.jvm.javaio.copyTo
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.io.core.Input import kotlinx.io.core.Input
import kotlinx.io.core.IoBuffer import kotlinx.io.core.IoBuffer
import kotlinx.io.core.buildPacket import kotlinx.io.core.buildPacket
import kotlinx.io.core.copyTo
import kotlinx.io.errors.IOException import kotlinx.io.errors.IOException
import kotlinx.io.streams.asInput import kotlinx.io.streams.asInput
import kotlinx.io.streams.asOutput
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.io.File import java.io.File
import java.io.InputStream import java.io.InputStream
@ -81,9 +84,9 @@ suspend fun File.suspendToExternalImage(): ExternalImage = withContext(IO) { toE
@Throws(IOException::class) @Throws(IOException::class)
fun URL.toExternalImage(): ExternalImage { fun URL.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() } val file = createTempFile().apply { deleteOnExit() }
file.outputStream().use { output -> file.outputStream().asOutput().use { output ->
openStream().use { input -> openStream().asInput().use { input ->
input.transferTo(output) input.copyTo(output)
} }
} }
return file.toExternalImage() return file.toExternalImage()
@ -101,8 +104,8 @@ suspend fun URL.suspendToExternalImage(): ExternalImage = withContext(IO) { toEx
@Throws(IOException::class) @Throws(IOException::class)
fun InputStream.toExternalImage(): ExternalImage { fun InputStream.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() } val file = createTempFile().apply { deleteOnExit() }
file.outputStream().use { file.outputStream().asOutput().use {
this.transferTo(it) this.asInput().copyTo(it)
} }
this.close() this.close()
return file.toExternalImage() return file.toExternalImage()
@ -120,8 +123,8 @@ suspend fun InputStream.suspendToExternalImage(): ExternalImage = withContext(IO
@Throws(IOException::class) @Throws(IOException::class)
fun Input.toExternalImage(): ExternalImage { fun Input.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() } val file = createTempFile().apply { deleteOnExit() }
file.outputStream().use { file.outputStream().asOutput().use {
this.asStream().transferTo(it) this.asStream().asInput().copyTo(it)
} }
return file.toExternalImage() return file.toExternalImage()
} }

View File

@ -4,6 +4,10 @@ package net.mamoe.mirai.utils
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO 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.DataInput
import java.io.EOFException import java.io.EOFException
import java.io.InputStream import java.io.InputStream
@ -21,13 +25,13 @@ actual fun md5(byteArray: ByteArray): ByteArray = MessageDigest.getInstance("MD5
fun InputStream.md5(): ByteArray { fun InputStream.md5(): ByteArray {
val digest = MessageDigest.getInstance("md5") val digest = MessageDigest.getInstance("md5")
digest.reset() digest.reset()
this.transferTo(object : OutputStream() { this.asInput().copyTo(object : OutputStream() {
override fun write(b: Int) { override fun write(b: Int) {
b.toByte().let { b.toByte().let {
digest.update(it) digest.update(it)
} }
} }
}) }.asOutput())
return digest.digest() return digest.digest()
} }