Can someone please point me to an example of decom...
# announcements
t
Can someone please point me to an example of decompressing a zlib file idiomatically
k
t
Yeah I see a lot of java example but I would like to look at idiomatic kotlin code
k
It's exactly the same.
t
Oh alright. Thanks 🙂. Expected it to be much cleaner. Like iterating over iis instead of for loop
k
Ah you're asking about reading an inputstream to a string? I think there's something neater for that indeed, let me check.
t
Take a compressed ByteArray and generate decompressed String/ByteArray
k
Sorry for not getting back to you sooner, there's
InputStream.transferTo
for this. The whole thing looks like this now:
Copy code
val source = ByteArray(100)

val inStream = ByteArrayInputStream(source)
val defStream = InflaterInputStream(inStream)

val outStream = ByteArrayOutputStream()
defStream.transferTo(outStream)

val array = outStream.toByteArray()
val string = String(array, Charsets.UTF_8)
t
Awesome. No need to close these streams?
k
Wel behind the scenes that's what
transferTo
does, but sure you don't need to do it yourself.
t
Nice. Thanks alot 🙂