forked from shaogezhu/easy-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加了常见得序列化技术(hessian2、kryo、jdk、fastjson)
- Loading branch information
Showing
16 changed files
with
324 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/RpcInvocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/SerializeFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.shaogezhu.easy.rpc.core.serialize; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/3/4 | ||
* @description: 序列化的接口 | ||
*/ | ||
public interface SerializeFactory { | ||
|
||
|
||
/** | ||
* 序列化 | ||
* | ||
* @param t | ||
* @param <T> | ||
* @return | ||
*/ | ||
<T> byte[] serialize(T t); | ||
|
||
/** | ||
* 反序列化 | ||
* | ||
* @param data | ||
* @param clazz | ||
* @param <T> | ||
* @return | ||
*/ | ||
<T> T deserialize(byte[] data, Class<T> clazz); | ||
} |
24 changes: 24 additions & 0 deletions
24
...rc/main/java/com/shaogezhu/easy/rpc/core/serialize/fastjson/FastJsonSerializeFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.shaogezhu.easy.rpc.core.serialize.fastjson; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/3/4 | ||
* @description: FastJson序列化工厂 | ||
*/ | ||
public class FastJsonSerializeFactory implements SerializeFactory { | ||
|
||
@Override | ||
public <T> byte[] serialize(T t) { | ||
String jsonStr = JSON.toJSONString(t); | ||
return jsonStr.getBytes(); | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(byte[] data, Class<T> clazz) { | ||
return JSON.parseObject(new String(data),clazz); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
.../src/main/java/com/shaogezhu/easy/rpc/core/serialize/hessian/HessianSerializeFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.shaogezhu.easy.rpc.core.serialize.hessian; | ||
|
||
import com.caucho.hessian.io.Hessian2Input; | ||
import com.caucho.hessian.io.Hessian2Output; | ||
import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/3/4 | ||
* @description: Hessian序列化工厂 | ||
*/ | ||
public class HessianSerializeFactory implements SerializeFactory { | ||
|
||
@Override | ||
public <T> byte[] serialize(T t) { | ||
byte[] data = null; | ||
try { | ||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
Hessian2Output output = new Hessian2Output(os); | ||
output.writeObject(t); | ||
output.getBytesOutputStream().flush(); | ||
output.completeMessage(); | ||
output.close(); | ||
data = os.toByteArray(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return data; | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(byte[] data, Class<T> clazz) { | ||
if (data == null) { | ||
return null; | ||
} | ||
Object result = null; | ||
try { | ||
ByteArrayInputStream is = new ByteArrayInputStream(data); | ||
Hessian2Input input = new Hessian2Input(is); | ||
result = input.readObject(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return (T) result; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/jdk/JdkSerializeFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.shaogezhu.easy.rpc.core.serialize.jdk; | ||
|
||
import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory; | ||
|
||
import java.io.*; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/3/4 | ||
* @description: JDK序列化工厂 | ||
*/ | ||
public class JdkSerializeFactory implements SerializeFactory { | ||
|
||
|
||
@Override | ||
public <T> byte[] serialize(T t) { | ||
byte[] data = null; | ||
try { | ||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
ObjectOutputStream output = new ObjectOutputStream(os); | ||
output.writeObject(t); | ||
output.flush(); | ||
output.close(); | ||
data = os.toByteArray(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return data; | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(byte[] data, Class<T> clazz) { | ||
ByteArrayInputStream is = new ByteArrayInputStream(data); | ||
try { | ||
ObjectInputStream input = new ObjectInputStream(is); | ||
Object result = input.readObject(); | ||
return ((T) result); | ||
} catch (IOException | ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.