#
luxiaotao1123
2025-02-12 71eeac34fee9f5a53168e0872e5fb7b855c0b4c8
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
package com.vincent.rsf.common.utils;
 
import java.io.*;
 
public class Serialize {
 
    // 序列化
    public static byte[] serialize(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    // 反序列化
    public static Object unSerialize(byte[] bytes) {
        ByteArrayInputStream bais = null;
        try {
 
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais){
                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc)
                        throws IOException, ClassNotFoundException {
                    return Class.forName( desc.getName(), true, Thread.currentThread().getContextClassLoader());
                }
            };
            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
}