RPC-java/version6/krpc-common/src/main/java/common/serializer/myserializer/HessianSerializer.java
2025-02-13 16:39:06 +08:00

51 lines
1.7 KiB
Java

package common.serializer.myserializer;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import common.exception.SerializeException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @ClassName HessianSerializer
* @Description Hessian序列化
* @Author Tong
* @LastChangeDate 2024-11-29 11:49
* @Version v5.0
*/
public class HessianSerializer implements Serializer {
@Override
public byte[] serialize(Object obj) {
// 使用 ByteArrayOutputStream 和 HessianOutput 来实现对象的序列化
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
HessianOutput hessianOutput = new HessianOutput(byteArrayOutputStream);
hessianOutput.writeObject(obj); // 将对象写入输出流
return byteArrayOutputStream.toByteArray(); // 返回字节数组
} catch (IOException e) {
throw new SerializeException("Serialization failed");
}
}
@Override
public Object deserialize(byte[] bytes, int messageType) {
// 使用 ByteArrayInputStream 和 HessianInput 来实现反序列化
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
HessianInput hessianInput = new HessianInput(byteArrayInputStream);
return hessianInput.readObject(); // 读取并返回对象
} catch (IOException e) {
throw new SerializeException("Deserialization failed");
}
}
@Override
public int getType() {
return 3;
}
@Override
public String toString() {
return "Hessian";
}
}