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

com.xxl.rpc.serialize.impl.HessianSerializer Maven / Gradle / Ivy

package com.xxl.rpc.serialize.impl;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.xxl.rpc.serialize.Serializer;
import com.xxl.rpc.util.XxlRpcException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * hessian serialize
 * @author xuxueli 2015-9-26 02:53:29
 */
public class HessianSerializer extends Serializer {

	@Override
	public  byte[] serialize(T obj){
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Hessian2Output ho = new Hessian2Output(os);
		try {
			ho.writeObject(obj);
			ho.flush();
			byte[] result = os.toByteArray();
			return result;
		} catch (IOException e) {
			throw new XxlRpcException(e);
		} finally {
			try {
				ho.close();
			} catch (IOException e) {
				throw new XxlRpcException(e);
			}
			try {
				os.close();
			} catch (IOException e) {
				throw new XxlRpcException(e);
			}
		}

	}

	@Override
	public  Object deserialize(byte[] bytes, Class clazz) {
		ByteArrayInputStream is = new ByteArrayInputStream(bytes);
		Hessian2Input hi = new Hessian2Input(is);
		try {
			Object result = hi.readObject();
			return result;
		} catch (IOException e) {
			throw new XxlRpcException(e);
		} finally {
			try {
				hi.close();
			} catch (Exception e) {
				throw new XxlRpcException(e);
			}
			try {
				is.close();
			} catch (IOException e) {
				throw new XxlRpcException(e);
			}
		}
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy