• Java 序列化对象工具类


    SerializationUtils.java

    package javax.utils;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * 序列化工具类
     * 
     * @author Logan
     * @version 1.0.0
     *
     */
    public class SerializationUtils {
    
        /**
         * 序列化对象
         * 
         * @param obj 对象
         * @return 序列化后的字节数组
         * @throws IOException
         */
        public static byte[] serialize(Object obj) throws IOException {
            if (null == obj) {
                return null;
            }
    
            try (
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream);
            ) {
    
                out.writeObject(obj);
                return byteArrayOutputStream.toByteArray();
            }
    
        }
    
        /**
         * 反序列化
         * 
         * @param bytes 对象序列化后的字节数组
         * @return 反序列化后的对象
         * @throws IOException
         * @throws ClassNotFoundException
         */
        public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
            if (null == bytes) {
                return null;
            }
    
            try (
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
                    ObjectInputStream in = new ObjectInputStream(byteArrayInputStream);
            ) {
    
                return in.readObject();
            }
        }
    
        /**
         * 反序列化成指定类型的对象
         * 
         * @param bytes 对象序列化后的字节数组
         * @param c 反序列化后的对象类型
         * @return 指定类型的对象
         * @throws IOException
         * @throws ClassNotFoundException
         */
        public static <T> T deserialize(byte[] bytes, Class<T> c) throws ClassNotFoundException, IOException {
            return c.cast(deserialize(bytes));
        }
    
        public static void main(String[] args) {
            try {
                Map<String, Object> map = new HashMap<>();
                map.put("name", "Logan");
                map.put("age", 26);
    
                byte[] bytes = serialize(map);
                Map<?, ?> map2 = deserialize(bytes, Map.class);
                System.out.println(map2);
    
                System.out.println(map.equals(map2));
                System.out.println(map == map2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }

     

    .

  • 相关阅读:
    Java从入门到精通——调错篇之ORACLE 打开PLSQL时提示ora-01033
    2012年——2013年总结
    一个订单相关的存储过程(MySQL)
    EHCache 实现通用类 CacheManager
    超越算法来看待个性化推荐
    Jackson怎样转换这样的字符串? String jsonStr = "{dataType:'Custom',regexp:'t\d+',msg:'输入不正确'}";
    实时个性化推荐系统简述
    Java从入门到精通——基础篇之Servlet与JSP的区别
    TIME_WAIT引起Cannot assign requested address报错
    synchronized 用法,实例讲解
  • 原文地址:https://www.cnblogs.com/jonban/p/10290090.html
Copyright © 2020-2023  润新知