mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-03 00:49:49 +08:00
Simplify platform structure
This commit is contained in:
parent
3fb25a07ae
commit
ff9702a992
@ -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<E>() : List<E>, Queue<E>, Deque<E>
|
|
||||||
|
|
||||||
internal interface Queue<E> : MutableCollection<E> {
|
|
||||||
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<E> : Queue<E> {
|
|
||||||
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<E>
|
|
||||||
}
|
|
@ -10,27 +10,100 @@
|
|||||||
package net.mamoe.mirai.internal.utils
|
package net.mamoe.mirai.internal.utils
|
||||||
|
|
||||||
import io.ktor.client.*
|
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 {
|
internal object MiraiPlatformUtils {
|
||||||
fun unzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray
|
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. 不同平台使用不同引擎.
|
* 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<Byte>` and `ByteArray`
|
@Suppress("DuplicatedCode") // false positive. `this` is not the same for `List<Byte>` and `ByteArray`
|
||||||
|
@ -14,13 +14,15 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.internal.utils
|
package net.mamoe.mirai.internal.utils
|
||||||
|
|
||||||
import kotlin.jvm.JvmMultifileClass
|
|
||||||
import kotlin.jvm.JvmName
|
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
|
||||||
@PublishedApi
|
@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")
|
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
|
@ -6,9 +6,5 @@
|
|||||||
*
|
*
|
||||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||||
*/
|
*/
|
||||||
package net.mamoe.mirai.internal.utils
|
|
||||||
|
|
||||||
import java.util.LinkedList
|
package net.mamoe.mirai.internal
|
||||||
|
|
||||||
@Suppress("ACTUAL_WITHOUT_EXPECT")
|
|
||||||
internal actual typealias LinkedList<E> = LinkedList<E>
|
|
@ -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()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user