提交 cc8c6480 编写于 作者: K KevinJavaLee

Hessian序列化方法

上级 ba985636
......@@ -25,6 +25,8 @@
<slf4j.version>1.7.25</slf4j.version>
<!-- protostuff -->
<protostuff.version>1.7.2</protostuff.version>
<!--hessian-->
<hessian.version>4.0.65</hessian.version>
</properties>
<modules>
<module>rpc-framework-simple</module>
......
......@@ -52,5 +52,10 @@
<artifactId>protostuff-runtime</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>${hessian.version}</version>
</dependency>
</dependencies>
</project>
package github.javaguide.serialize.hessian;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import github.javaguide.exception.SerializeException;
import github.javaguide.serialize.Serializer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* Hessian is a dynamically-typed, binary serialization and Web Services protocol designed for object-oriented transmission.
*
* @author Vinlee Xiao
* @createTime 2022/2/23 21:11
*/
public class HessianSerializer implements Serializer {
@Override
public byte[] serialize(Object obj) {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
HessianOutput hessianOutput = new HessianOutput(byteArrayOutputStream);
hessianOutput.writeObject(obj);
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
throw new SerializeException("Serialization failed");
}
}
@Override
public <T> T deserialize(byte[] bytes, Class<T> clazz) {
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
HessianInput hessianInput = new HessianInput(byteArrayInputStream);
Object o = hessianInput.readObject();
return clazz.cast(o);
} catch (Exception e) {
throw new SerializeException("Deserialization failed");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册