hjg
2024-07-09 4fd62ace17390b0b9cc37c55e88582134ec18c28
提交 | 用户 | 时间
58d006 1 package com.mandi.common;
A 2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.ObjectInputStream;
6 import java.io.ObjectOutputStream;
7
8 import org.apache.log4j.Logger;
9
10
11 /** 
12  * @author mengly 
13  * @version 创建时间:2016年2月27日 下午5:08:30 
14  * 类说明 
15  */
16
17 public class SerializeMethod {
18     private static Logger log=Logger.getLogger(SerializeMethod.class);
19     /**
20      * 序列化
21      * 
22      * @param object
23      * @return
24      */
25     public static <T> byte[] serialize(T object) {
26         ObjectOutputStream oos = null;
27         ByteArrayOutputStream baos = null;
28         try {
29             baos = new ByteArrayOutputStream();
30             oos = new ObjectOutputStream(baos);
31             oos.writeObject(object);
32             byte[] bytes = baos.toByteArray();
33             return bytes;
34         } catch (Exception e) {
35             log.info("序列化错误"+e.getMessage());
36         }
37         return null;
38     }
39
40     /**
41      * 反序列化
42      * 
43      * @param bytes
44      * @return
45      */
46     public static <T> T  deserialize(byte[] bytes,Class<T> clazz) {
47         ByteArrayInputStream bais = null;
48         try {
49             bais = new ByteArrayInputStream(bytes);
50             ObjectInputStream ois = new ObjectInputStream(bais);
51             Object obj=ois.readObject();
52             @SuppressWarnings("unchecked")
53             T t=(T)obj;
54             return t;
55         } catch (Exception e) {
56             log.info("反序列化错误"+e.getMessage());
57         }
58         return null;
59     }
60 }