package org.springblade.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializableUtil {

	//序列化
	public static String serializeToString(Object obj) throws Exception{
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
		objOut.writeObject(obj);
		String str = byteOut.toString("ISO-8859-1");//此处只能是ISO-8859-1,但是不会影响中文使用
		return str;
	}
	//反序列化
	public static Object deserializeToObject(String str) throws Exception{
		ByteArrayInputStream byteIn = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
		ObjectInputStream objIn = new ObjectInputStream(byteIn);
		Object obj =objIn.readObject();
		return obj;
	}
}
