https://kotlinlang.org logo
Title
m

Mani

07/26/2019, 7:53 PM
Hi guys, a naive doubt, please bear with me. Any good example with gzipping API responses in Kotlin?
c

Casey Brooks

07/26/2019, 7:58 PM
Ktor has compression built-in to its core, just needs to be enabled https://ktor.io/servers/features/compression.html
For any other framework, I’m sure there are standard compression mechanisms there as well. It’s not really a concern of the Kotlin language, but the framework you’re using
m

Mani

07/26/2019, 7:59 PM
spring-boot
z

zpearce

07/26/2019, 8:01 PM
There's a #spring channel
f

Felipe Assoline

07/26/2019, 9:06 PM
Spring Boot has compression too. Just a yml/properties configuration.
server.compression.enabled: true
I always check built in features viewing this doc: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
👍 1
m

Mani

07/26/2019, 9:07 PM
fun gzip(content: String): ByteArray {
        val bos = ByteArrayOutputStream()
        GZIPOutputStream(bos).bufferedWriter(UTF_8).use { it.write(content) }
        return bos.toByteArray()
    }
I used this piece of code. The response times are crazy.
t

tipsy

07/26/2019, 10:49 PM
GZIPOutputStream(res.outputStream, true).use { gzippedStream ->
    content.copyTo(gzippedStream)
}
just write it directly to the client