package cgl.iotcloud.core.utils;
import org.apache.thrift.TBase;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
import java.io.*;
public class SerializationUtils {
public static byte[] serializeToBytes(Serializable object) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(object);
return bos.toByteArray();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {
// ignore close exception
}
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
}
public static Object createFromBytes(byte []bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
return in.readObject();
} finally {
try {
bis.close();
} catch (IOException ex) {
// ignore close exception
}
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}
}
public static byte[] serializeThriftObject(TBase object) throws TException {
return new TSerializer().serialize(object);
}
public static void createThriftFromBytes(byte []bytes, TBase object) throws TException {
new TDeserializer().deserialize(object, bytes);
}
}