import java.io.*;
/**
* Created by jingqing.zhou on 2015/6/12.
* ByteArrayOutputStream :可以捕获内存缓冲区的数据,转换成字节数组。
* DataInputStream&DataOutputStream关心如何将数据从高层次的形式转化成低层次的形式.
* FileInputStream&FileOutputStream关心如何操作存储单元以接受和产生数据。
*/
public class ByteArrayIO {
//序列化对象为String字符串,先对序列化后的结果进行BASE64编码,否则不能直接进行反序列化
public static String writeObject(Object o) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(o);
oos.flush();
oos.close();
bos.close();
//return new BASE64Encoder().encode(bos.toByteArray());
return new String(bos.toByteArray(), "ISO-8859-1");
}
//反序列化String字符串为对象
public static Object readObject(String object) throws Exception{
//ByteArrayInputStream bis = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(object));
ByteArrayInputStream bis = new ByteArrayInputStream(object.getBytes("ISO-8859-1"));
ObjectInputStream ois = new ObjectInputStream(bis);
Object o = null;
try {
o = ois.readObject();
} catch(EOFException e) {
System.err.print("read finished");
}
bis.close();
ois.close();
return o;
}
}