diff --git a/whatsmars-lightrpc/lightrpc-serialization/pom.xml b/whatsmars-lightrpc/lightrpc-serialization/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..69185a5ab1551400f75fb82d2bf3a3f492d7d46f --- /dev/null +++ b/whatsmars-lightrpc/lightrpc-serialization/pom.xml @@ -0,0 +1,26 @@ + + + + lightrpc + org.hongxi + 1.0-SNAPSHOT + + 4.0.0 + + lightrpc-serialization + jar + ${project.artifactId} + http://maven.apache.org + + + + com.caucho + hessian + 4.0.38 + + + + + \ No newline at end of file diff --git a/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/codec/Serialization.java b/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/codec/Serialization.java new file mode 100644 index 0000000000000000000000000000000000000000..8924012384dfef1f500997e3126370a8ffc088e6 --- /dev/null +++ b/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/codec/Serialization.java @@ -0,0 +1,23 @@ +package org.hongxi.lightrpc.codec; + +import java.io.IOException; + +/** + * Created by shenhongxi on 2018/10/7. + */ +public interface Serialization { + + byte[] serialize(Object obj) throws IOException; + + T deserialize(byte[] bytes, Class clz) throws IOException; + + byte[] serializeMulti(Object[] data) throws IOException; + + Object[] deserializeMulti(byte[] data, Class[] classes) throws IOException; + + /** + * serializaion的唯一编号,用于传输协议中指定序列化方式。每种序列化的编号必须唯一。 + * @return 由于编码规范限制,序列化方式最大支持32种,因此返回值必须在0-31之间。 + */ + int getSerializationNumber(); +} diff --git a/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/serialization/DeserializableObject.java b/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/serialization/DeserializableObject.java new file mode 100644 index 0000000000000000000000000000000000000000..053c884f15b01d54fb3395042afe47d362150dff --- /dev/null +++ b/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/serialization/DeserializableObject.java @@ -0,0 +1,30 @@ +package org.hongxi.lightrpc.serialization; + +import org.hongxi.lightrpc.codec.Serialization; + +import java.io.IOException; + +/** + * Created by shenhongxi on 2018/10/7. + */ +public class DeserializableObject { + private Serialization serialization; + private byte[] objBytes; + + public DeserializableObject(Serialization serialization, byte[] objBytes) { + this.serialization = serialization; + this.objBytes = objBytes; + } + + public T deserialize(Class clz) throws IOException { + return serialization.deserialize(objBytes, clz); + } + + public Object[] deserializeMulti(Class[] paramTypes) throws IOException { + Object[] ret = null; + if (paramTypes != null && paramTypes.length > 0) { + ret = serialization.deserializeMulti(objBytes, paramTypes); + } + return ret; + } +} diff --git a/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/serialization/Hessian2Serialization.java b/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/serialization/Hessian2Serialization.java new file mode 100644 index 0000000000000000000000000000000000000000..16a86bb7a10c7cf62c3150b3ccd3cdf13c328138 --- /dev/null +++ b/whatsmars-lightrpc/lightrpc-serialization/src/main/java/org/hongxi/lightrpc/serialization/Hessian2Serialization.java @@ -0,0 +1,57 @@ +package org.hongxi.lightrpc.serialization; + +import com.caucho.hessian.io.Hessian2Input; +import com.caucho.hessian.io.Hessian2Output; +import org.hongxi.lightrpc.codec.Serialization; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * Created by shenhongxi on 2018/10/7. + */ +public class Hessian2Serialization implements Serialization { + + @Override + public byte[] serialize(Object data) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + Hessian2Output out = new Hessian2Output(bos); + out.writeObject(data); + out.flush(); + return bos.toByteArray(); + } + + @SuppressWarnings("unchecked") + @Override + public T deserialize(byte[] data, Class clz) throws IOException { + Hessian2Input input = new Hessian2Input(new ByteArrayInputStream(data)); + return (T) input.readObject(clz); + } + + @Override + public byte[] serializeMulti(Object[] data) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + Hessian2Output out = new Hessian2Output(bos); + for(Object obj: data){ + out.writeObject(obj); + } + out.flush(); + return bos.toByteArray(); + } + + @Override + public Object[] deserializeMulti(byte[] data, Class[] classes) throws IOException { + Hessian2Input input = new Hessian2Input(new ByteArrayInputStream(data)); + Object[] objects = new Object[classes.length]; + for (int i = 0; i < classes.length; i++) { + objects[i] = input.readObject(classes[i]); + } + return objects; + } + + @Override + public int getSerializationNumber() { + return 0; + } +} diff --git a/whatsmars-lightrpc/pom.xml b/whatsmars-lightrpc/pom.xml index 86081995c42e411ec2565e886090aec8580db0ab..ef1d41ca46ea927731c1ca1e6ad03994df238896 100644 --- a/whatsmars-lightrpc/pom.xml +++ b/whatsmars-lightrpc/pom.xml @@ -15,6 +15,7 @@ lightrpc-rpc lightrpc-remoting + lightrpc-serialization