How to Compress a Java Object

Recently i’m developing a simple client server application, using serialized java objects as parameter. To increase performance, im trying to compress my java object. Here’s how i do it.

a simple serialized java bean

package com.edw.bean;

import java.io.Serializable;
import java.math.BigDecimal;

public class TestBean implements Serializable {
    private BigDecimal id;
    private String value;

    public BigDecimal getId() {
        return id;
    }

    public void setId(BigDecimal id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

and a class to compress my object

package com.edw.main;

import com.edw.bean.TestBean;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.log4j.Logger;

public class Main {

    private final static Logger logger = Logger.getLogger(Main.class);

    public Main() {
    }

    private void startApp() throws Exception {
        // create a very big bean
        TestBean tb = new TestBean();
        tb.setId(new BigDecimal(Double.MAX_VALUE));
        String bigString = "";
        for (int i = 0; i < 5000; i++) {
            bigString += " test " + i;
        }
        tb.setValue(bigString);

        // write it to file to see this object's size
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("test.size"), false));
        oos.writeObject(tb);
        oos.flush();
        oos.close();

        // get the serialized object
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("test.size")));
        Object regularObject = ois.readObject();
        logger.debug(((TestBean) regularObject).getId());

        // write compressed object to file to see this object's size
        FileOutputStream fos = new FileOutputStream(new File("testCompressed.size"), false);
        GZIPOutputStream gzipos = new GZIPOutputStream(fos) {

            {
                def.setLevel(Deflater.BEST_COMPRESSION);
            }
        };

        ObjectOutputStream oosCompressed = new ObjectOutputStream(gzipos);
        oosCompressed.writeObject(tb);
        oosCompressed.flush();
        oosCompressed.close();

        // get the serialized object
        FileInputStream fis = new FileInputStream(new File("testCompressed.size"));
        GZIPInputStream gzipis = new GZIPInputStream(fis);
        ObjectInputStream oisCompressed = new ObjectInputStream(gzipis);
        Object compressedObject = oisCompressed.readObject();
        logger.debug(((TestBean) compressedObject).getId());
    }

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.startApp();
    }
}

it can compress up to 75% of size,

Thank you and have a good time coding. (*)

Google+

No Comments

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)