Commit 1e5dcb0e authored by Him188's avatar Him188

Replace all transferTo to Kotlinx transfer, downgrade JDK requirement

parent a2e82e75
......@@ -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
......
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
......@@ -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()
}
......
......@@ -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()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment