admin
2021-10-13 052e1d5c47c4e536fde79074d53b0481c7d4f9b6
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
package org.yeshi.utils;
 
import java.io.*;
 
/**
 * @Description: 序列化反序列化工具
 */
public class SerializeUtil {
    /**
     * 序列化
     */
    public static byte[] serialize(Object obj) {
 
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
 
        try {
            //序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
 
            oos.writeObject(obj);
            byte[] byteArray = baos.toByteArray();
            return byteArray;
 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 反序列化
     *
     * @param bytes
     * @return
     */
    public static Object unSerialize(byte[] bytes) {
 
        ByteArrayInputStream bais = null;
 
        try {
            //反序列化为对象
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}