All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.huanminabc.utils_common.obj.serializable.SerializeUtil Maven / Gradle / Ivy

There is a newer version: 1.0.5-RELEASE
Show newest version
package com.gitee.huanminabc.utils_common.obj.serializable;



import com.gitee.huanminabc.utils_common.base.UniversalException;
import com.gitee.huanminabc.utils_common.file.ReadFileBytesUtil;
import com.gitee.huanminabc.utils_common.file.WriteFileBytesUtil;

import java.io.*;

//对象序列化
public class SerializeUtil {
    public static byte[] serialize(Object 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) {
             UniversalException.logError(e);
        }
        return null;
    }

    public static void serializeToFile(Object object, File read) {
        byte[] serialize = serialize(object);
        WriteFileBytesUtil.writeByte(serialize, read, false);
    }

    public static  T unserialize(byte[] bytes, Class tClass) {
        ByteArrayInputStream bais = null;
        try {
            // 反序列化
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return (T) ois.readObject();
        } catch (Exception e) {
           UniversalException.logError(e);

        }
        return null;
    }

    public static  T readUnserialize(File read, Class tClass) {
        byte[] bytes = ReadFileBytesUtil.readByte(read);
        return unserialize(bytes, tClass);

    }


}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy