diff --git a/mirai-core/src/commonMain/kotlin/utils/LinkedList.common.kt b/mirai-core/src/commonMain/kotlin/utils/LinkedList.common.kt deleted file mode 100644 index 498b3d04b..000000000 --- a/mirai-core/src/commonMain/kotlin/utils/LinkedList.common.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2019-2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.internal.utils - -// We target JVM and Android only. -internal expect class LinkedList() : List, Queue, Deque - -internal interface Queue : MutableCollection { - override fun add(element: E): Boolean - fun offer(element: E): Boolean - fun remove(): E - fun poll(): E - fun element(): E - fun peek(): E -} - -internal interface Deque : Queue { - fun addFirst(e: E) - fun addLast(e: E) - fun offerFirst(e: E): Boolean - fun offerLast(e: E): Boolean - fun removeFirst(): E - fun removeLast(): E - fun pollFirst(): E - fun pollLast(): E - val first: E - val last: E - fun peekFirst(): E - fun peekLast(): E - fun removeFirstOccurrence(o: E): Boolean - fun removeLastOccurrence(o: E): Boolean - fun push(e: E) - fun pop(): E - fun descendingIterator(): Iterator -} \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/utils/MiraiPlatformUtils.common.kt b/mirai-core/src/commonMain/kotlin/utils/MiraiPlatformUtils.common.kt index 6e4ca5c75..fbcc813e1 100644 --- a/mirai-core/src/commonMain/kotlin/utils/MiraiPlatformUtils.common.kt +++ b/mirai-core/src/commonMain/kotlin/utils/MiraiPlatformUtils.common.kt @@ -10,27 +10,100 @@ package net.mamoe.mirai.internal.utils import io.ktor.client.* +import io.ktor.client.engine.cio.* +import io.ktor.util.* +import kotlinx.io.pool.useInstance +import java.io.ByteArrayOutputStream +import java.io.InputStream +import java.io.OutputStream +import java.net.Inet4Address +import java.security.MessageDigest +import java.util.zip.Deflater +import java.util.zip.GZIPInputStream +import java.util.zip.GZIPOutputStream +import java.util.zip.Inflater -internal expect object MiraiPlatformUtils { - fun unzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray +internal object MiraiPlatformUtils { + fun unzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray { + data.checkOffsetAndLength(offset, length) + if (length == 0) return ByteArray(0) - fun zip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray + val inflater = Inflater() + inflater.reset() + ByteArrayOutputStream().use { output -> + inflater.setInput(data, offset, length) + ByteArrayPool.useInstance { + while (!inflater.finished()) { + output.write(it, 0, inflater.inflate(it)) + } + } - fun gzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray + inflater.end() + return output.toByteArray() + } + } - fun ungzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray + fun zip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray { + data.checkOffsetAndLength(offset, length) + if (length == 0) return ByteArray(0) + val deflater = Deflater() + deflater.setInput(data, offset, length) + deflater.finish() - fun md5(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray + ByteArrayPool.useInstance { + return it.take(deflater.deflate(it)).toByteArray().also { deflater.end() } + } + } - inline fun md5(str: String): ByteArray + fun gzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray { + ByteArrayOutputStream().use { buf -> + GZIPOutputStream(buf).use { gzip -> + data.inputStream(offset, length).use { t -> t.copyTo(gzip) } + } + buf.flush() + return buf.toByteArray() + } + } - fun localIpAddress(): String + fun ungzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray { + return GZIPInputStream(data.inputStream(offset, length)).use { it.readBytes() } + } + + fun md5(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray { + data.checkOffsetAndLength(offset, length) + return MessageDigest.getInstance("MD5").apply { update(data, offset, length) }.digest() + } + + fun md5(str: String): ByteArray = md5(str.toByteArray()) /** * Ktor HttpClient. 不同平台使用不同引擎. */ - val Http: HttpClient + @OptIn(KtorExperimentalAPI::class) + val Http: HttpClient = HttpClient(CIO) + + /** + * Localhost 解析 + */ + fun localIpAddress(): String = kotlin.runCatching { + Inet4Address.getLocalHost().hostAddress + }.getOrElse { "192.168.1.123" } + + fun md5(stream: InputStream): ByteArray { + val digest = MessageDigest.getInstance("md5") + digest.reset() + stream.use { input -> + object : OutputStream() { + override fun write(b: Int) { + digest.update(b.toByte()) + } + }.use { output -> + input.copyTo(output) + } + } + return digest.digest() + } } @Suppress("DuplicatedCode") // false positive. `this` is not the same for `List` and `ByteArray` diff --git a/mirai-core/src/commonMain/kotlin/utils/tryNTimes.kt b/mirai-core/src/commonMain/kotlin/utils/tryNTimes.kt index bb93d76cd..f6e57bd97 100644 --- a/mirai-core/src/commonMain/kotlin/utils/tryNTimes.kt +++ b/mirai-core/src/commonMain/kotlin/utils/tryNTimes.kt @@ -14,13 +14,15 @@ package net.mamoe.mirai.internal.utils -import kotlin.jvm.JvmMultifileClass -import kotlin.jvm.JvmName import kotlin.reflect.KClass - @PublishedApi -internal expect fun Throwable.addSuppressedMirai(e: Throwable) +internal fun Throwable.addSuppressedMirai(e: Throwable) { + if (e === this) return + kotlin.runCatching { + this.addSuppressed(e) + } +} @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") @kotlin.internal.InlineOnly diff --git a/mirai-core/src/jvmMain/kotlin/utils/LinkedList.kt b/mirai-core/src/jvmMain/kotlin/package.kt similarity index 70% rename from mirai-core/src/jvmMain/kotlin/utils/LinkedList.kt rename to mirai-core/src/jvmMain/kotlin/package.kt index 1bbd954b4..d0d089745 100644 --- a/mirai-core/src/jvmMain/kotlin/utils/LinkedList.kt +++ b/mirai-core/src/jvmMain/kotlin/package.kt @@ -6,9 +6,5 @@ * * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package net.mamoe.mirai.internal.utils -import java.util.LinkedList - -@Suppress("ACTUAL_WITHOUT_EXPECT") -internal actual typealias LinkedList = LinkedList \ No newline at end of file +package net.mamoe.mirai.internal \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/utils/MiraiPlatformUtils.kt b/mirai-core/src/jvmMain/kotlin/utils/MiraiPlatformUtils.kt deleted file mode 100644 index 734e46c9b..000000000 --- a/mirai-core/src/jvmMain/kotlin/utils/MiraiPlatformUtils.kt +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2019-2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:Suppress("NOTHING_TO_INLINE") - -package net.mamoe.mirai.internal.utils - -import io.ktor.client.* -import io.ktor.client.engine.cio.* -import io.ktor.util.* -import kotlinx.io.pool.useInstance -import java.io.ByteArrayOutputStream -import java.io.InputStream -import java.io.OutputStream -import java.net.Inet4Address -import java.security.MessageDigest -import java.util.zip.Deflater -import java.util.zip.GZIPInputStream -import java.util.zip.GZIPOutputStream -import java.util.zip.Inflater - -internal actual object MiraiPlatformUtils { - actual fun unzip(data: ByteArray, offset: Int, length: Int): ByteArray { - data.checkOffsetAndLength(offset, length) - if (length == 0) return ByteArray(0) - - val inflater = Inflater() - inflater.reset() - ByteArrayOutputStream().use { output -> - inflater.setInput(data, offset, length) - ByteArrayPool.useInstance { - while (!inflater.finished()) { - output.write(it, 0, inflater.inflate(it)) - } - } - - inflater.end() - return output.toByteArray() - } - } - - actual fun zip(data: ByteArray, offset: Int, length: Int): ByteArray { - data.checkOffsetAndLength(offset, length) - if (length == 0) return ByteArray(0) - - val deflater = Deflater() - deflater.setInput(data, offset, length) - deflater.finish() - - ByteArrayPool.useInstance { - return it.take(deflater.deflate(it)).toByteArray().also { deflater.end() } - } - } - - actual fun gzip(data: ByteArray, offset: Int, length: Int): ByteArray { - ByteArrayOutputStream().use { buf -> - GZIPOutputStream(buf).use { gzip -> - data.inputStream(offset, length).use { t -> t.copyTo(gzip) } - } - buf.flush() - return buf.toByteArray() - } - } - - actual fun ungzip(data: ByteArray, offset: Int, length: Int): ByteArray { - return GZIPInputStream(data.inputStream(offset, length)).use { it.readBytes() } - } - - actual fun md5(data: ByteArray, offset: Int, length: Int): ByteArray { - data.checkOffsetAndLength(offset, length) - return MessageDigest.getInstance("MD5").apply { update(data, offset, length) }.digest() - } - - actual inline fun md5(str: String): ByteArray = md5(str.toByteArray()) - - /** - * Ktor HttpClient. 不同平台使用不同引擎. - */ - @OptIn(KtorExperimentalAPI::class) - actual val Http: HttpClient = HttpClient(CIO) - - /** - * Localhost 解析 - */ - actual fun localIpAddress(): String = runCatching { - Inet4Address.getLocalHost().hostAddress - }.getOrElse { "192.168.1.123" } - - fun md5(stream: InputStream): ByteArray { - val digest = MessageDigest.getInstance("md5") - digest.reset() - stream.use { input -> - object : OutputStream() { - override fun write(b: Int) { - digest.update(b.toByte()) - } - }.use { output -> - input.copyTo(output) - } - } - return digest.digest() - } - -} \ No newline at end of file diff --git a/mirai-core/src/jvmMain/kotlin/utils/addSuppressedMirai.kt b/mirai-core/src/jvmMain/kotlin/utils/addSuppressedMirai.kt deleted file mode 100644 index 36cfa2464..000000000 --- a/mirai-core/src/jvmMain/kotlin/utils/addSuppressedMirai.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2019-2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.internal.utils - -@PublishedApi -internal actual fun Throwable.addSuppressedMirai(e: Throwable) { - if (e === this) { - return - } - kotlin.runCatching { - this.addSuppressed(e) - } -} \ No newline at end of file