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