package cn.com.qmth.stmms.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

public class HessianUtils {

    public static String serialize(Object obj) {
        if (obj == null)
            throw new NullPointerException();
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            HessianOutput ho = new HessianOutput(os);
            ho.writeObject(obj);
            return new String(os.toByteArray(), "ISO-8859-1");
        } catch (Exception e) {
            return "";
        }
    }

    public static Object deserialize(String source) {
        try {
            ByteArrayInputStream is = new ByteArrayInputStream(source.getBytes("ISO-8859-1"));
            HessianInput hi = new HessianInput(is);
            return hi.readObject();
        } catch (Exception e) {
            return null;
        }
    }
}
