• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

best way to compress in GZIP serialized objects in MEMORY

 
Amer Seifeddine
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am looking for sample code to compress a serialized object in the memory (in a form of ByteArray or whatever) Say, Object o is the serialized object and i need to turn it into another Object z (compressed) as another variable. Do you have a sample?

Thanks!
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to do this? Serialized object are in a binary format; I think it's unlikely that they can be compressed to any great degree.
 
Amer Seifeddine
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, that is not correct.. serialized objects are zipped up to 90% or more. do you have some sample code?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amer is right, actually. My experience with Jess's binary dump files is that GZIP can do about a factor of 5 on complex object graphs.

But in any case, serializing to a compressed byte array just looks like

ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(new ByteArrayOutputStream(), 10000)));
oos.writeObject(myObject);
oos.close();
[ May 06, 2008: Message edited by: Ernest Friedman-Hill ]
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, convert the serialized Object to an array of bytes, eg using a ByteArrayOutputStream, then run it through java.util.zip.Deflater to compress it.
See the Jave API documentation on how to use java.util.zip.Deflater (for compression) and java.util.zip.Inflater (for decompression). You'll also find code samples there.
 
Amer Seifeddine
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. This is usefull to persist user session in memory if inactive instead of writing it to a file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic