wood burning stoves 2.0*
The moose likes Java in General and the fly likes best way to compress in GZIP serialized objects in MEMORY Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "best way to compress in GZIP serialized objects in MEMORY" Watch "best way to compress in GZIP serialized objects in MEMORY" New topic
Author

best way to compress in GZIP serialized objects in MEMORY

Amer Seifeddine
Ranch Hand

Joined: Apr 25, 2002
Posts: 57
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
Marshal

Joined: Mar 22, 2005
Posts: 41042
    
  43
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.


Ping & DNS - my free Android networking tools app
Amer Seifeddine
Ranch Hand

Joined: Apr 25, 2002
Posts: 57
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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

Joined: Apr 25, 2002
Posts: 57
Thanks. This is usefull to persist user session in memory if inactive instead of writing it to a file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: best way to compress in GZIP serialized objects in MEMORY
 
Similar Threads
transient
How to access data in another servlet
Why Object needs to be serialized while sending it over network
Converting Object to String
Weird question