zoukankan      html  css  js  c++  java
  • byte[]与Object转化,hessian序列化/反序列化

    【1】Object >> byte[].

    约束:该对象必须可序列化,且其包含的各属性对象,也必须可序列化

    注意:注释掉的部分为hessian实现方式

     

        public static byte[] objectToBytes(Object obj){
            byte[] result = null;
            ByteArrayOutputStream byteOutputStream = null;
            ObjectOutputStream objectOutputStream = null;
           
    //        Hessian2Output objectOutputStream2 = null;
           
            try{
                byteOutputStream = new ByteArrayOutputStream();
                objectOutputStream = new ObjectOutputStream(byteOutputStream);
               
                objectOutputStream.writeObject(obj);
                objectOutputStream.flush();
               
    //            objectOutputStream2.startMessage();
    //            objectOutputStream2.writeObject(obj);
    //            objectOutputStream2.completeMessage();
               
                result = byteOutputStream.toByteArray();
            }catch(Exception e){
                e.printStackTrace();
            } finally {
                if(null != objectOutputStream){
                    try{
                        objectOutputStream.close();
                        byteOutputStream.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }     
           
            return result;
        }

     

    【2】byte[] >> Object

    约束:如果byte[]里的类型与所转类型不同,则会抛异常java.lang.ClassCastException

    注意:注释掉的部分为hessian实现方式

     

        public static Object bytesToObject(byte[] bytes){
           
            Object result = null;
            ByteArrayInputStream byteInputStream = null;
            ObjectInputStream objectInputStream = null;
           
    //        Hessian2Input objectInputStream2 = null;
           
            try{
                byteInputStream = new ByteArrayInputStream(bytes);
                objectInputStream = new ObjectInputStream(byteInputStream);
               
                result = objectInputStream.readObject();
               
    //            objectInputStream2.startMessage();
    //            result = objectInputStream2.readObject();
    //            objectInputStream2.completeMessage();
               
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                if(null != objectInputStream){
                    try{
                        objectInputStream.close();
                        byteInputStream.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
           
            return result;
        }

  • 相关阅读:
    Spring Boot 2.4 手工和 SDKMAN! 安装 Spring Boot 命令行
    Spring Boot 2.4 安装
    Java Web 开发中的中文乱码与解决方式
    项目文件与 SVN 资源库同步提示错误 Attempted to lock an already-locked dir
    JavaScript中的日期时间函数
    jmeter测试 常用数据库的性能
    jmeter 运行多个sql
    Linux 进程管理
    Linux vi 文件编辑
    Linux 常用文件管理命令
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/2563556.html
Copyright © 2011-2022 走看看