Add gzip and ungzip

This commit is contained in:
Him188 2020-03-28 21:53:33 +08:00
parent 8f2843cbfa
commit 5672837878
4 changed files with 43 additions and 1 deletions

View File

@ -21,6 +21,8 @@ import java.io.InputStream
import java.net.Inet4Address import java.net.Inet4Address
import java.security.MessageDigest import java.security.MessageDigest
import java.util.zip.Deflater import java.util.zip.Deflater
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import java.util.zip.Inflater import java.util.zip.Inflater
@ -63,6 +65,7 @@ actual object MiraiPlatformUtils {
} }
} }
actual fun md5(data: ByteArray, offset: Int, length: Int): ByteArray { actual fun md5(data: ByteArray, offset: Int, length: Int): ByteArray {
data.checkOffsetAndLength(offset, length) data.checkOffsetAndLength(offset, length)
return MessageDigest.getInstance("MD5").apply { update(data, offset, length) }.digest() return MessageDigest.getInstance("MD5").apply { update(data, offset, length) }.digest()
@ -99,4 +102,18 @@ actual object MiraiPlatformUtils {
block(read) 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() }
}
} }

View File

@ -30,6 +30,10 @@ expect object MiraiPlatformUtils {
fun zip(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray 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 fun md5(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): ByteArray

View File

@ -14,11 +14,16 @@ import net.mamoe.mirai.utils.io.encodeToString
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@OptIn(MiraiInternalAPI::class)
internal class PlatformUtilsTest { internal class PlatformUtilsTest {
@OptIn(MiraiInternalAPI::class)
@Test @Test
fun testZip() { fun testZip() {
assertEquals("test", MiraiPlatformUtils.unzip(MiraiPlatformUtils.zip("test".toByteArray())).encodeToString()) assertEquals("test", MiraiPlatformUtils.unzip(MiraiPlatformUtils.zip("test".toByteArray())).encodeToString())
} }
@Test
fun testGZip() {
assertEquals("test", MiraiPlatformUtils.ungzip(MiraiPlatformUtils.gzip("test".toByteArray())).encodeToString())
}
} }

View File

@ -22,6 +22,8 @@ import java.io.OutputStream
import java.net.Inet4Address import java.net.Inet4Address
import java.security.MessageDigest import java.security.MessageDigest
import java.util.zip.Deflater import java.util.zip.Deflater
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import java.util.zip.Inflater 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 { actual fun md5(data: ByteArray, offset: Int, length: Int): ByteArray {
data.checkOffsetAndLength(offset, length) data.checkOffsetAndLength(offset, length)
return MessageDigest.getInstance("MD5").apply { update(data, offset, length) }.digest() return MessageDigest.getInstance("MD5").apply { update(data, offset, length) }.digest()