diff --git a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/platformAndroid.kt b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/platformAndroid.kt index 4e4de1eff..ea2c41cd1 100644 --- a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/platformAndroid.kt +++ b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/platformAndroid.kt @@ -21,6 +21,8 @@ import java.io.InputStream 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 @@ -63,6 +65,7 @@ actual object MiraiPlatformUtils { } } + actual fun md5(data: ByteArray, offset: Int, length: Int): ByteArray { data.checkOffsetAndLength(offset, length) return MessageDigest.getInstance("MD5").apply { update(data, offset, length) }.digest() @@ -99,4 +102,18 @@ actual object MiraiPlatformUtils { block(read) } } + + 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() } + } } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/platform.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/platform.kt index fd5f8a083..f40840a37 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/platform.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/platform.kt @@ -30,6 +30,10 @@ expect object MiraiPlatformUtils { fun zip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray + fun gzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray + + fun ungzip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray + fun md5(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray diff --git a/mirai-core/src/commonTest/kotlin/net/mamoe/mirai/utils/PlatformUtilsTest.kt b/mirai-core/src/commonTest/kotlin/net/mamoe/mirai/utils/PlatformUtilsTest.kt index 9bf4d397b..cead3f84e 100644 --- a/mirai-core/src/commonTest/kotlin/net/mamoe/mirai/utils/PlatformUtilsTest.kt +++ b/mirai-core/src/commonTest/kotlin/net/mamoe/mirai/utils/PlatformUtilsTest.kt @@ -14,11 +14,16 @@ import net.mamoe.mirai.utils.io.encodeToString import kotlin.test.Test import kotlin.test.assertEquals +@OptIn(MiraiInternalAPI::class) internal class PlatformUtilsTest { - @OptIn(MiraiInternalAPI::class) @Test fun testZip() { assertEquals("test", MiraiPlatformUtils.unzip(MiraiPlatformUtils.zip("test".toByteArray())).encodeToString()) } + + @Test + fun testGZip() { + assertEquals("test", MiraiPlatformUtils.ungzip(MiraiPlatformUtils.gzip("test".toByteArray())).encodeToString()) + } } \ No newline at end of file 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 e142b46d1..d5af19847 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 @@ -22,6 +22,8 @@ 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 /** @@ -64,6 +66,20 @@ actual object MiraiPlatformUtils { } } + 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()