aspose file tools*
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 Java 8 in Action this week in the Java 8 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: 39578
    
  27
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 - updated with new look and Ping home screen widget
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: 24168
    
  30

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
Converting Object to String
How to access data in another servlet
Weird question
transient
Why Object needs to be serialized while sending it over network