对象序列化。

1、利用jdk自带的 (ObjectOutPutStream.writeObject ,ObjectInputStream.readObject())

  1. public static byte[] ObjectToByte(java.lang.Object obj) {  
  2.     byte[] bytes = null;  
  3.     try {  
  4.         // object to bytearray  
  5.         ByteArrayOutputStream bo = new ByteArrayOutputStream();  
  6.         ObjectOutputStream oo = new ObjectOutputStream(bo);  
  7.         oo.writeObject(obj);  
  8.   
  9.         bytes = bo.toByteArray();  
  10.   
  11.         bo.close();  
  12.         oo.close();  
  13.     } catch (Exception e) {  
  14.         System.out.println("translation" + e.getMessage());  
  15.         e.printStackTrace();  
  16.     }  
  17.     return bytes;  
  18. }  
  1. public static Object ByteToObject(byte[] bytes) {  
  2. Object obj = null;  
  3. try {  
  4.     // bytearray to object  
  5.     ByteArrayInputStream bi = new ByteArrayInputStream(bytes);  
  6.     ObjectInputStream oi = new ObjectInputStream(bi);  
  7.   
  8.     obj = oi.readObject();  
  9.     bi.close();  
  10.     oi.close();  
  11. catch (Exception e) {  
  12.     System.out.println("translation" + e.getMessage());  
  13.     e.printStackTrace();  
  14. }  
  15.        return obj;  
  16.    }  
二:自定义对象的序列化与反序列化


public class ByteArraySerialize
{

    private static long START_TIME = 1264953600000L;// 2010.01.01 00:00:00
    private static int CAPACITY = 1024;
        private static byte VERSION_2 = 2;// 版本号 orderId
    private ThreadLocal<ByteBuffer> local = new ThreadLocal<ByteBuffer>() {
        protected ByteBuffer initialValue() {
            return ByteBuffer.allocate(CAPACITY);
        }
    };

  
    public byte[] serialize(PromiseResult promise) {
        if (promise == null)
            return new byte[0];
        ByteBuffer buf = local.get();
        buf.position(0);
        buf.put(VERSION_2);
        buf.putLong(promise.getOrderId());
      
        buf.putInt(promise.isShow() ? 1 : 0);
       
        if (promise.getPromiseDate() != null) {
            buf.putInt((int) ((promise.getPromiseDate().getTime() - START_TIME) / 1000));
        } else {
            buf.putInt(-1);
        }

        if (promise.getOrderEffectDate() != null) {

            buf.putInt((int) ((promise.getOrderEffectDate().getTime() - START_TIME) / 1000));
        } else {
            buf.putInt(-1);
        }

        if (promise.getOrderTime() != null) {
            buf.putInt((int) ((promise.getOrderTime().getTime() - START_TIME) / 1000));
        } else {
            buf.putInt(-1);
        }

        if (promise.getResultType() != null) {
            buf.putInt(promise.getResultType());
        } else {
            buf.putInt(-1);
        }
        // deal with the message
        int length = 0;
        byte[] by = null;
       final String message = promise.getMessage();
       if (message != null && !StringUtils.equals("", message)) {
            try {
                by = message.getBytes("UTF-8");
                length = by.length;
                buf.putInt(length);
                buf.put(by);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        } else {
            buf.putInt(-1);
        }
        byte[] result = new byte[buf.position()];
        buf.position(0);
        buf.get(result, 0, result.length);
        return result;
    }

    public PromiseResult deserialize(byte[] bytes) {
        if (bytes.length == 0) {
            return null;
        }

        PromiseResult promise = new PromiseResult();

        ByteBuffer buf = local.get();
        buf.position(0);
        buf.put(bytes);
        buf.position(0);
        byte version = buf.get();
       Long orderId = buf.getLong();
       promise.setOrderId(orderId);
       
        int isshow = buf.getInt();
        promise.setShow(isshow ==  1 ?  true : false);

        int time = buf.getInt();
        if (time != -1) {
            promise.setPromiseDate(new Date(time * 1000L + START_TIME));
        } else {
            promise.setPromiseDate(null);
        }

        time = buf.getInt();
        if (time != -1) {
            promise.setOrderEffectDate(new Date(time * 1000L + START_TIME));
        } else {
            promise.setOrderEffectDate(null);
        }

        time = buf.getInt();
        if (time != -1) {
            promise.setOrderTime(new Date(time * 1000L + START_TIME));
        } else {
            promise.setOrderTime(null);
        }

        int resultType = buf.getInt();
        if (resultType != -1) {
            promise.setResultType(resultType);
        }

        // deal with the message
        int length = buf.getInt();
        if(length != -1) {
            byte[] dst = new byte[length];
            buf.get(dst);
            try {
                promise.setMessage(new String(dst, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
            }
        } else {
            promise.setMessage(null);
        }

        return promise;
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值