All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.j2mvc.framework.dao.callback.StreamUtil Maven / Gradle / Ivy

Go to download

强烈建议使用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

There is a newer version: 2.1.12
Show newest version
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;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy