mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-25 12:21:32 +02:00
Refactors the use of Gzip
This commit is contained in:
@@ -23,9 +23,7 @@ package com.vitorpamplona.quartz.nip19Bech32.entities
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip19Bech32.toNEmbed
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.util.zip.GZIPInputStream
|
||||
import java.util.zip.GZIPOutputStream
|
||||
import com.vitorpamplona.quartz.utils.GZip
|
||||
|
||||
@Immutable
|
||||
data class NEmbed(
|
||||
@@ -34,18 +32,9 @@ data class NEmbed(
|
||||
companion object {
|
||||
fun parse(bytes: ByteArray): NEmbed? {
|
||||
if (bytes.isEmpty()) return null
|
||||
return NEmbed(Event.fromJson(ungzip(bytes)))
|
||||
return NEmbed(Event.fromJson(GZip.decompress(bytes)))
|
||||
}
|
||||
|
||||
fun create(event: Event): String = gzip(event.toJson()).toNEmbed()
|
||||
|
||||
fun gzip(content: String): ByteArray {
|
||||
val bos = ByteArrayOutputStream()
|
||||
GZIPOutputStream(bos).bufferedWriter(Charsets.UTF_8).use { it.write(content) }
|
||||
val array = bos.toByteArray()
|
||||
return array
|
||||
}
|
||||
|
||||
fun ungzip(content: ByteArray): String = GZIPInputStream(content.inputStream()).bufferedReader(Charsets.UTF_8).use { it.readText() }
|
||||
fun create(event: Event): String = GZip.compress(event.toJson()).toNEmbed()
|
||||
}
|
||||
}
|
||||
|
37
quartz/src/main/java/com/vitorpamplona/quartz/utils/GZip.kt
Normal file
37
quartz/src/main/java/com/vitorpamplona/quartz/utils/GZip.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.utils
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.util.zip.GZIPInputStream
|
||||
import java.util.zip.GZIPOutputStream
|
||||
|
||||
class GZip {
|
||||
companion object {
|
||||
fun compress(content: String): ByteArray {
|
||||
val bos = ByteArrayOutputStream()
|
||||
GZIPOutputStream(bos).bufferedWriter(Charsets.UTF_8).use { it.write(content) }
|
||||
return bos.toByteArray()
|
||||
}
|
||||
|
||||
fun decompress(content: ByteArray): String = GZIPInputStream(content.inputStream()).bufferedReader(Charsets.UTF_8).use { it.readText() }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user