• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

best way to compress in GZIP serialized objects in MEMORY

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. This is usefull to persist user session in memory if inactive instead of writing it to a file.
 
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic