com.j2mvc.framework.dao.callback.StreamUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of j2mvc-framework-app Show documentation
Show all versions of j2mvc-framework-app Show documentation
强烈建议使用J2mvc 2.1以后的版本。
version 2.1.01
1.更换JSON依赖包.
version 2.1.02
1.移除com.j2mvc.StringUtils.getUtf8()方法调用.
更改为getCharset()
version 2.1.03
1.更新JNDI连接设置
version 2.1.04
1.修改works.xml配置url-pkg-prefixes改为pkg
package com.j2mvc.framework.dao.callback;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.apache.log4j.Logger;
public class StreamUtil {
static Logger log = Logger.getLogger(StreamUtil.class.getName());
//将对象转换为byte[]
public static byte[] objectToBytes(Object obj){
ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
ObjectOutputStream outObj;
try {
outObj = new ObjectOutputStream(byteOut);
outObj.writeObject(obj) ;
} catch (IOException e) {
log.error(e.getMessage());
}
return byteOut.toByteArray();
}
//将byte[]转换为对象
public static Object bytesToObject(byte[] buff) {
try {
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(buff)));
return in.readObject();
} catch (IOException e) {
log.error(e.getMessage());
return null;
}catch (ClassNotFoundException e) {
log.error(e.getMessage());
return null;
}
}
}