With this example we are going to demonstrate how to compress and expand an Object. We have implemented the ObjectCompressionUtil
Class, that extends the Serializable class and has a parameter that is the generic type of the serializable object to be compressed. The class consists of two methods.
- The
- Creates a new GZIPOutputStream with a default buffer size.
- Creates an ObjectOutputStream that writes to the specified GZIPOutputStream.
- Writes the specified object to be compressed to the ObjectOutputStream and flush it, using
writeObject(Object obj)
andflush()
API methods of ObjectOutputStream. - Closes both the GZIPOutputStream and the ObjectOutputStream, using their
close()
API methods.
compressObject(final T objectToCompress, final OutputStream outstream)
takes the object to compress and an OutputStream and returns the compressed object, as described below:- The
- Creates a new GZIPInputStream with a default buffer size.
- Creates an ObjectInputStream that reads from the specified GZIPInputStream.
- Reads the object to expand from the ObjectInputStream, with
readObject()
API method of ObjectInputStream. - Returns the expanded object and closes both the GZIPInputStream and the ObjectInputStream, using their
close()
API methods.
expandObject(final T objectToExpand, final InputStream instream)
method takes the object to expand and an InputStream and returns the expanded object, as shown below:Let’s take a look at the code snippet that follows:
package javaitzen.blog; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * The Class ObjectCompressionUtil. * * @param <T> the generic type of the serializable object to be compressed */ public class ObjectCompressionUtil<T extends Serializable> { /** * Compress object. * * @param objectToCompress the object to compress * @param outstream the outstream * @return the compressed object * @throws IOException Signals that an I/O exception has occurred. */ public T compressObject(final T objectToCompress, final OutputStream outstream) throws IOException { final GZIPOutputStream gz = new GZIPOutputStream(outstream); final ObjectOutputStream oos = new ObjectOutputStream(gz); try { oos.writeObject(objectToCompress); oos.flush(); return objectToCompress; }finally { oos.close(); outstream.close(); } } /** * Expand object. * * @param objectToExpand the object to expand * @param instream the instream * @return the expanded object * @throws IOException Signals that an I/O exception has occurred. * @throws ClassNotFoundException the class not found exception */ public T expandObject(final T objectToExpand, final InputStream instream) throws IOException, ClassNotFoundException { final GZIPInputStream gs = new GZIPInputStream(instream); final ObjectInputStream ois = new ObjectInputStream(gs); try { @SuppressWarnings("unchecked") T expandedObject = (T) ois.readObject(); return expandedObject; } finally { gs.close(); ois.close(); } } }
Related Article:
Reference: Java Compression from our JCG partner Brian at Zen in the art of IT
This was an example of how to compress and expand an Object in Java.