All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ltsopensource.remoting.serialize.JavaSerializable Maven / Gradle / Ivy

package com.github.ltsopensource.remoting.serialize;

import com.github.ltsopensource.core.commons.io.UnsafeByteArrayInputStream;
import com.github.ltsopensource.core.commons.io.UnsafeByteArrayOutputStream;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Robert HG ([email protected]) on 11/6/15.
 */
public class JavaSerializable implements RemotingSerializable {

    @Override
    public int getId() {
        return 3;
    }

    @Override
    public byte[] serialize(Object obj) throws Exception {

        UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);

        try {
            oos.writeObject(obj);
            oos.flush();
            return bos.toByteArray();
        } finally {
            oos.close();
        }
    }

    @SuppressWarnings("unchecked")
	@Override
    public  T deserialize(byte[] data, Class clazz) throws Exception {

        UnsafeByteArrayInputStream bin = new UnsafeByteArrayInputStream(data);
        ObjectInputStream ois = new ObjectInputStream(bin);

        try {
            Object obj = ois.readObject();
            return (T) obj;
        } finally {
            ois.close();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy