zoukankan      html  css  js  c++  java
  • java之数组和对象的互转

    java
     * 对象转bytes和bytes转对象
     * 
     * @project order
     * @fileName ByteUtil.java
     * @Description
     * @author light-zhang
     * @date 2019年5月16日
     * @version 1.0.0
     */
    public class ByteUtil {
        /**
         * 对象转数组
         * 
         * @param obj
         * @return
         */
        public static byte[] serialize(Object object) {
            byte[] bytes = null;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                ObjectOutputStream oos = new ObjectOutputStream(bos);
                oos.writeObject(object);
                oos.flush();
                bytes = CompressUtil.compress(bos.toByteArray());// 在这里对byte压缩
                oos.close();
                bos.close();
            } catch (IOException ex) {
                Assert.RuntimeException("Object转byte[]出现错误");
                ex.printStackTrace();
            }
            return bytes;
        }
    
        /**
         * 数组转对象
         * 
         * @param bytes
         * @return
         */
        public static Object deserialize(byte[] bytes) {
            Object object = null;
            try {
                ByteArrayInputStream bis = new ByteArrayInputStream(CompressUtil.uncompress(bytes));// 在这里解压
                ObjectInputStream ois = new ObjectInputStream(bis);
                object = ois.readObject();
                ois.close();
                bis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                Assert.RuntimeException("byte[]转对象错误");
                ex.printStackTrace();
            }
            return object;
        }
    
    }
    
  • 相关阅读:
    OD: Memory Attach Technology
    Chrome: Shockwave Flash isn't responding
    OD: Memory Attach Technology
    OD: Heap Exploit : DWORD Shooting & Opcode Injecting
    OD: Heap in Windows 2K & XP SP1
    OD: Writing Small Shellcode
    OD: Shellcode Encoding
    Set Windows IP by Batch
    OD: Universal Shellcode
    XenServer 使用笔记
  • 原文地址:https://www.cnblogs.com/caoxueyang/p/14251739.html
Copyright © 2011-2022 走看看