Kryo and compression, how does it work?

842 views
Skip to first unread message

mean mole

unread,
May 30, 2021, 6:00:04 PM5/30/21
to kryo-users
Hello,

I have read the Github manual page about compression but I can't figure how to make use of it properly.

Here's an example snippet from my app that works perfectly fine:

// serialize and write object to disk:
val writeFile = combinedColladaModel.serializedFile
val output = Output(FileOutputStream(writeFile))
modelSerializer.writeObject(output, sm)
output.close()
...
// later read the serialized file back as object:
val input = Input(FileInputStream(fileName!!))
val modelData: SerializedModel = k.readObject(input, SerializedModel::class.java)
input.close()

Now if I change the code to be like this:

// serialize and write object to disk: (OK):
val writeFile = combinedColladaModel.serializedFile
val output = Output(DeflaterOutputStream(FileOutputStream(writeFile)))
modelSerializer.writeObject(output, sm)
output.close()
...
// later read the serialized file back as object (CRASH):
val input = Input(DeflaterInputStream(FileInputStream(fileName!!)))
val modelData: SerializedModel = k.readObject(input, SerializedModel::class.java)
input.close()

The object gets compressed and serialized to disk (the file on the disk now has significantly smaller size) but when I try to read it back to my application from the disk, as shown in the code above, I get "KryoException: Buffer underflow".
Is this the correct way to use the compression? Are there any code examples online showing how object compression, serialization and deserialization can be done with Kryo?

Thanks! 

Thomas Heigl

unread,
May 31, 2021, 4:36:14 AM5/31/21
to kryo-...@googlegroups.com
Hi,

I think you have to use `InflaterInputStream` instead of `DeflaterInputStream` when reading the compressed file. This would be a good addition to the Readme.

I have never used compression with `DeflaterOutputStream`. What I usually do is compress the byte[] that Kryo produces and decompress it again before deserialization. I strongly recommend using
https://github.com/lz4/lz4-java for this if you want optimal speed and compressed size.

Thomas


--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users
---
You received this message because you are subscribed to the Google Groups "kryo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryo-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kryo-users/07d7d509-5189-4c5b-b93d-da370262685bn%40googlegroups.com.

mean mole

unread,
May 31, 2021, 8:43:27 AM5/31/21
to kryo-users
Hey,

Thanks for pointing that out! Deflater / Inflater, makes sense I guess :) I don't have previous experience with these compression tools, but the compression works now as it should!
I will also check out the lz4-java later. 

Have a nice day!

Reply all
Reply to author
Forward
0 new messages