DataCollect

public class DataCollect implements Serializable
{
    /**
     * 序列化ID
     */
    private static final long serialVersionUID = 600000060L;


    /**
     * 日志对象
     */
    private static Logger logger = Logger.getLogger(DataCollect.class);


    /**
     * 消息ID
     */
    private int msgID = 0;


    /**
     * 对象参数,该对象里所有的对象都要序列化,否则传输转换不成功
     */
    private Object object = null;


    /**
     * 
     * 构造函数。
     * 
     * @param msgID 消息ID
     * @param object 对象参数
     */
    public DataCollect(int msgID, Object object)
    {
        this.msgID = msgID;
        this.object = object;
    }


    /**
     * 
     * 构造函数。
     * 
     */
    public DataCollect()
    {


    }


    /**
     * 获取消息ID。
     * 
     * @return 消息ID
     */
    public int getMsgID()
    {
        return msgID;
    }


    /**
     * 设置消息ID。
     * @param msgID 消息ID
     */
    public void setMsgID(int msgID)
    {
        this.msgID = msgID;
    }


    /**
     * 获取对象参数
     * 
     * @return object
     */
    public Object getObject()
    {
        return object;
    }


    /**
     * 设置对象参数。
     * 
     * @param object 对象参数
     */
    public void setObject(Object object)
    {
        this.object = object;
    }


    /**
     * 
     * 关闭输出流。
     * 
     * @param outputStream 输出流
     */
    private void closeOutputStream(OutputStream outputStream)
    {
        logger.recordEnterMethodLog();


        if (null == outputStream)
        {
            logger.recordExitMethodLog();
            return;
        }


        try
        {
            outputStream.close();
        }
        catch (IOException e)
        {
            logger.recordErrorsLog("close stream fail, stream is :"
                    + outputStream.getClass().getName());
        }


        logger.recordExitMethodLog();
    }


    /**
     * 
     * 关闭输入流。
     * 
     * @param inputStream 输入流
     */
    private void closeInputStream(InputStream inputStream)
    {
        logger.recordEnterMethodLog();


        if (null == inputStream)
        {
            logger.recordExitMethodLog();
            return;
        }


        try
        {
            inputStream.close();
        }
        catch (IOException e)
        {
            logger.recordErrorsLog("close stream fail, stream is :"
                    + inputStream.getClass().getName());
        }


        logger.recordExitMethodLog();
    }


    /**
    * 
    * 将byte输入流转换成DataCollect对象
    * 
    * @param bytes byte数组
    * @return 0:转换成功   其他:转换失败
    */
    public int revertPacket(byte[] bytes)
    {
        logger.recordEnterMethodLog();


        if (null == bytes || bytes.length <= 0)
        {
            logger.recordErrorsLog("revertPacket bytes is null");
            logger.recordExitMethodLog();
            return PortalCommonConst.RESULT_FAIL;
        }


        // 定义一个处理变量,0:成功 1:失败
        int result = PortalCommonConst.RESULT_SUCCESS;
        
        ByteArrayInputStream byteArrayInputStream = null;
        
        ObjectInputStream objectInputStream = null;
        try
        {
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            if (null != objectInputStream)
            {
                DataCollect dataCollect = (DataCollect) objectInputStream.readObject();
                if (null != dataCollect)
                {
                    this.msgID = dataCollect.getMsgID();
                    this.object = dataCollect.getObject();
                }
            }
        }
        catch (IOException e)
        {
            result = PortalCommonConst.RESULT_FAIL;
            logger.recordErrorsLog("readObject fail");
        }
        catch (ClassNotFoundException e)
        {
            result = PortalCommonConst.RESULT_FAIL;
            logger.recordErrorsLog("class is not found, class is DataCollect");
        }
        finally
        {
            // 数据转换成功后,关闭创建的输入流
            closeInputStream(byteArrayInputStream);
            closeInputStream(objectInputStream);
        }
        logger.recordExitMethodLog();
        return result;
    }


    /**
     * 
     * 将数据汇聚DataCollect对象转换为byte输出流
     * 
     * @return 0:转换成功  非0:转换失败
     */
    public byte[] addPacket()
    {
        logger.recordEnterMethodLog();


        byte[] outBytes = null;
        ObjectOutputStream objectOutputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;


        boolean isSuccess = true;
        try
        {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            if (null != objectOutputStream)
            {
                objectOutputStream.writeObject(this);
            }
        }
        catch (IOException e)
        {
            isSuccess = false;
            logger.recordErrorsLog(e.toString());
        }
        finally
        {
            if (isSuccess)
            {
                outBytes = byteArrayOutputStream.toByteArray();
            }


            // 数据转换成功后,关闭创建的输出流
            closeOutputStream(byteArrayOutputStream);
            closeOutputStream(objectOutputStream);
        }
        logger.recordExitMethodLog();
        return outBytes;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值