Java对象序列化 与 反序列化

本文介绍了一种实现对象与字节流互相转换的方法,包括将对象序列化为ByteBuffer以及从ByteBuffer中反序列化出原始对象的过程。该方法使用了Java的序列化API,如ObjectInputStream和ObjectOutputStream,并记录了序列化过程中的异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

import org.apache.log4j.Logger;

/**
* <b>Summary: </b>
 *      对象与流的相互转换
* <b>Remarks: </b>
 *        对象与流的相互转换
*/
publicclass ObjectStream {   

    privatefinalstatic Logger log = Logger.getLogger(ObjectStream.class);

    /**
     * <b>Summary: </b>
     *     toByteBuffer(对象转 byte 数组)
     * @param object
     * @return
     */
    publicstaticbyte[] toByteBuffer(Object object) {
        ByteArrayOutputStream bio =new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        ByteBuffer outBuf = null;
        try {
            oos =new ObjectOutputStream(bio);
            oos.writeObject(object);
            byte[] ba = bio.toByteArray();
            int len = ba.length;
            outBuf = ByteBuffer.allocate(len);
            outBuf.put(ba);
            outBuf.flip();
        } catch (IOException e) {
            log.error("对象序列化异常[IOException]:"+e.getMessage());
            e.printStackTrace();            
        } finally {
            try {
                if (bio != null)
                    bio.close();
                if (oos != null)
                    oos.close();
            } catch (IOException e) {
                log.error("对象序列化,关闭流异常[IOException]:"+e.getMessage());
                e.printStackTrace();                
            }
        }
        return outBuf.array();
    }

    /**
     * <b>Summary: </b>
     *     toObject(byte 数组转对象)
     * @param buff
     * @return
     */
    publicstatic Object toObject(byte[] buff) {
        ByteArrayInputStream bis =new ByteArrayInputStream(buff);
        ObjectInputStream ois = null;
        Object object = null;
        try {
            ois =new ObjectInputStream(bis);
            object = ois.readObject();
        } catch (IOException e) {
            log.error("对象反序列化异常[IOException]:"+e.getMessage());
            e.printStackTrace();            
        } catch (ClassNotFoundException e) {
            log.error("对象反序列化异常[ClassNotFoundException]:"+e.getMessage());
            e.printStackTrace();            
        } finally {
            try {
                if (bis != null)
                    bis.close();
                if (ois != null)
                    ois.close();
            } catch (IOException e) {
                log.error("对象反序列化,关闭流异常[IOException]:"+e.getMessage());
                e.printStackTrace();                
            }
        }
        return object;
    }
}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值