派生自 projectDept/qhighschool

yn147
2023-11-23 bccada7cbf7eea3c37c0243d95426d1a29d9121f
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
61
62
63
64
65
66
67
package com.qxueyou.scc.base.util;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
public class ReisUtils {
    private static Logger log = LogManager.getLogger(ReisUtils.class);
    
    /**
     * 序列化对象
     */
    public static byte[] serialize(Object obj){
        ObjectOutputStream objectOs = null;
        ByteArrayOutputStream byteArrOs = null;
        byte[] bytes = null;
        try {
            byteArrOs = new ByteArrayOutputStream();
            objectOs = new ObjectOutputStream(byteArrOs);
            objectOs.writeObject(obj);
            bytes = byteArrOs.toByteArray();
        } catch (Exception e) {
            log.error("序列化对象失败!");
        }finally {
            IOUtils.closeQuietly(objectOs);
            IOUtils.closeQuietly(byteArrOs);
        }
        return bytes;
    }
 
    /**
     * 反序列化对象
     */
    public static Object unserialize(byte[] bytes){
        Object returnObject = null;
        ByteArrayInputStream byteArrIns = null;
        ObjectInputStream objectIns = null;
        try {
            if (bytes != null && bytes.length!=0){
                byteArrIns = new ByteArrayInputStream(bytes);
                objectIns = new ObjectInputStream(byteArrIns);
                returnObject = objectIns.readObject();
            }
        }catch (Exception e) {
            log.error("反序列化对象失败!",e);
        }finally {
            IOUtils.closeQuietly(objectIns);
            IOUtils.closeQuietly(byteArrIns);
        }
        return returnObject;
    }
    
 
//    public static void main(String[] args) throws IOException{
//        Object s = null;
//         byte[] b = serialize(s);
//        Object obj= unserialize(b);
//        System.out.println(s);
//    }
    
    
}