Skip to content
Advertisement

Kotlin gzip uncompress fail

I try to simplify my java gzip uncompress code to kotlin. But after I changed, it sames broken.

Here is the java code

JavaScript

This is my kotlin code.

JavaScript

And I got this error.

JavaScript

It seems that the decompression process was interrupted by reader?

Advertisement

Answer

The readText(charset: Charset = Charsets.UTF_8) decodes the bytes into UTF-8 character set, which is why it says “This could mean either than the input has been truncated” it probably have tried to convert 8-bits into a Char and build a String out of it.

Use the readBytes() to get ByteArray which is represented same as byte[] in JVM platform.

Example:

JavaScript

Edit:

For reading bytes, you shouldn’t be using the Reader, it is meant for reading the Text in UTF-8 format as defined in the Kotlin’s InputStream.bufferedReader:

JavaScript

The InputStream.readBytes() will read the bytes at a buffer of 8KB itself.

JavaScript

So you just have to do:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement