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;
}
}
{
/**
* 序列化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;
}
}