JAVA 对象 & byte数组之间转化

 对象 & byte数组之间转化工具类
效果图:objectToByteArray: [B@7d4991ad
byteArrayToObject: WXOrderMsgEntity(uuid=null, orderSeq=W2018052100000002, carportSeq=1001, stakeSeq=102)
{"carportSeq":1001,"orderSeq":"W2018052100000002","stakeSeq":102}
package com.extracme.echarge;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.extracme.evready.charge.define.WXOrderMsgEntity;

public class TestHalp {

	public static void main(String[] args) {
		WXOrderMsgEntity msg=new WXOrderMsgEntity();
		msg.setCarportSeq(1001);
		msg.setOrderSeq("W2018052100000002");
		msg.setStakeSeq(102);
		
		byte[] objectToByteArray = objectToByteArray(msg);
        System.err.println("objectToByteArray: " + objectToByteArray);
        
		Message message=new Message();
		message.setBody(objectToByteArray);
		
		Object byteArrayToObject = byteArrayToObject(message.getBody());
		System.err.println("byteArrayToObject: " + byteArrayToObject.toString());
		WXOrderMsgEntity d=(WXOrderMsgEntity)byteArrayToObject;
		System.out.println(JSON.toJSONString(d));
	}

	    /**
	     * 对象转Byte数组
	     * @param obj
	     * @return
	     */
	    public static byte[] objectToByteArray(Object obj) {
	        byte[] bytes = null;
	        ByteArrayOutputStream byteArrayOutputStream = null;
	        ObjectOutputStream objectOutputStream = null;
	        try {
	            byteArrayOutputStream = new ByteArrayOutputStream();
	            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
	            objectOutputStream.writeObject(obj);
	            objectOutputStream.flush();
	            bytes = byteArrayOutputStream.toByteArray();

	        } catch (IOException e) {
	            System.err.println("objectToByteArray failed, " + e);
	        } finally {
	            if (objectOutputStream != null) {
	                try {
	                    objectOutputStream.close();
	                } catch (IOException e) {
	                    System.err.println("close objectOutputStream failed, " + e);
	                }
	            }
	            if (byteArrayOutputStream != null) {
	                try {
	                    byteArrayOutputStream.close();
	                } catch (IOException e) {
	                    System.err.println("close byteArrayOutputStream failed, " + e);
	                }
	            }

	        }
	        return bytes;
	    }

	    /**
	     * Byte数组转对象
	     * @param bytes
	     * @return
	     */
	    public static Object byteArrayToObject(byte[] bytes) {
	        Object obj = null;
	        ByteArrayInputStream byteArrayInputStream = null;
	        ObjectInputStream objectInputStream = null;
	        try {
	            byteArrayInputStream = new ByteArrayInputStream(bytes);
	            objectInputStream = new ObjectInputStream(byteArrayInputStream);
	            obj = objectInputStream.readObject();
	        } catch (Exception e) {
	            System.err.println("byteArrayToObject failed, " + e);
	        } finally {
	            if (byteArrayInputStream != null) {
	                try {
	                    byteArrayInputStream.close();
	                } catch (IOException e) {
	                    System.err.println("close byteArrayInputStream failed, " + e);
	                }
	            }
	            if (objectInputStream != null) {
	                try {
	                    objectInputStream.close();
	                } catch (IOException e) {
	                    System.err.println("close objectInputStream failed, " + e);
	                }
	            }
	        }
	        return obj;
	    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值