• SerializeUtil 序列化工具


    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.Closeable;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 序列化,反序列化工具
     * 
     * @project iweixin-pay
     * @fileName SerializeUtil.java
     * @Description
     * @author light-zhang
     * @date 2018年5月31日上午9:51:25
     * @version 1.0.0
     */
    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) {
                e.printStackTrace();
            } finally {
                close(oos);
                close(baos);
            }
            return null;
        }
    
        public static Object unserialize(byte[] bytes) {
            ByteArrayInputStream bais = null;
            ObjectInputStream ois = null;
            try {
                // 反序列化
                bais = new ByteArrayInputStream(bytes);
                ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close(bais);
                close(ois);
            }
            return null;
        }
    
        /**
         * 序列化 list 集合
         * 
         * @param list
         * @return
         */
        public static byte[] serializeList(List<?> list) {
    
            if (list == null || list.size() <= 0) {
                return null;
            }
            ObjectOutputStream oos = null;
            ByteArrayOutputStream baos = null;
            byte[] bytes = null;
            try {
                baos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(baos);
                for (Object obj : list) {
                    oos.writeObject(obj);
                }
                bytes = baos.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close(oos);
                close(baos);
            }
            return bytes;
        }
    
        /**
         * 反序列化 list 集合
         * 
         * @param lb
         * @return
         */
        public static List<?> unserializeList(byte[] bytes) {
            if (bytes == null) {
                return null;
            }
    
            List<Object> list = new ArrayList<Object>();
            ByteArrayInputStream bais = null;
            ObjectInputStream ois = null;
            try {
                // 反序列化
                bais = new ByteArrayInputStream(bytes);
                ois = new ObjectInputStream(bais);
                while (bais.available() > 0) {
                    Object obj = (Object) ois.readObject();
                    if (obj == null) {
                        break;
                    }
                    list.add(obj);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close(bais);
                close(ois);
            }
            return list;
        }
    
        /**
         * 关闭io流对象
         * 
         * @param closeable
         */
        public static void close(Closeable closeable) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    Sqlserver 存储过程游标中调用过程,过程中包含游标提示报错
    上位机和下位机的区别是什么
    VS(visual studio)中使用ReportViewer控件和报表设计器 RDLC
    SQLServer异常捕获
    PLC(可编程逻辑控制器)
    Sqlserver中的字符串相加变成NULL
    uniapp动态更改页面标题
    如何设置打印机双面打印?
    WebService相关
    ASP.NET无刷新客户端回调(通过实现ICallbackEventHandler接口)
  • 原文地址:https://www.cnblogs.com/light-zhang/p/9117890.html
Copyright © 2020-2023  润新知