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 e8a1157dd..54f7f60ba 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 @@ -54,18 +54,12 @@ actual object MiraiPlatformUtils { data.checkOffsetAndLength(offset, length) if (length == 0) return ByteArray(0) - val inflater = Deflater() - inflater.reset() - ByteArrayOutputStream().use { output -> - inflater.setInput(data, offset, length) - ByteArrayPool.useInstance { - while (!inflater.finished()) { - output.write(it, 0, inflater.deflate(it)) - } - } + val deflater = Deflater() + deflater.setInput(data, offset, length) + deflater.finish() - inflater.end() - return output.toByteArray() + ByteArrayPool.useInstance { + return it.take(deflater.deflate(it)).toByteArray().also { deflater.end() } } } 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 new file mode 100644 index 000000000..9bf4d397b --- /dev/null +++ b/mirai-core/src/commonTest/kotlin/net.mamoe.mirai.utils/PlatformUtilsTest.kt @@ -0,0 +1,24 @@ +/* + * Copyright 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.utils + +import kotlinx.io.core.toByteArray +import net.mamoe.mirai.utils.io.encodeToString +import kotlin.test.Test +import kotlin.test.assertEquals + +internal class PlatformUtilsTest { + + @OptIn(MiraiInternalAPI::class) + @Test + fun testZip() { + assertEquals("test", MiraiPlatformUtils.unzip(MiraiPlatformUtils.zip("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 1b316fe72..e40057fcd 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 @@ -55,18 +55,12 @@ actual object MiraiPlatformUtils { data.checkOffsetAndLength(offset, length) if (length == 0) return ByteArray(0) - val inflater = Deflater() - inflater.reset() - ByteArrayOutputStream().use { output -> - inflater.setInput(data, offset, length) - ByteArrayPool.useInstance { - while (!inflater.finished()) { - output.write(it, 0, inflater.deflate(it)) - } - } + val deflater = Deflater() + deflater.setInput(data, offset, length) + deflater.finish() - inflater.end() - return output.toByteArray() + ByteArrayPool.useInstance { + return it.take(deflater.deflate(it)).toByteArray().also { deflater.end() } } }