首页 > 学院 > 开发设计 > 正文

【Mrpc】 Demo1 对象与byte[]的相互转换

2019-11-14 09:41:12
字体:
来源:转载
供稿:网友

实现rpc框架时服务端与客户端之间的通讯发送的数据都是二进制数据,但实际上它们传送的内容都是一个个的对象。因此需要一个工具类来实现对象与byte[]的相互转换

代码资源已上传到http://download.csdn.net/detail/mrbcy/9747217

所用技术

我使用了PRotostuff框架来辅助进行转换操作。工具类代码如下:

ProtostuffUtil.java

package tech.mrbcy.mrpc.demo.demo1;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import com.dyuproject.protostuff.LinkedBuffer;import com.dyuproject.protostuff.ProtostuffIOUtil;import com.dyuproject.protostuff.Schema;import com.dyuproject.protostuff.runtime.RuntimeSchema;public class ProtostuffUtil { private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>(); private static <T> Schema<T> getSchema(Class<T> clazz) { @SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) cachedSchema.get(clazz); if (schema == null) { schema = RuntimeSchema.getSchema(clazz); if (schema != null) { cachedSchema.put(clazz, schema); } } return schema; } /** * 序列化 * * @param obj * @return */ public static <T> byte[] serializer(T obj) { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema<T> schema = getSchema(clazz); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } } /** * 反序列化 * * @param data * @param clazz * @return */ public static <T> T deserializer(byte[] data, Class<T> clazz) { try { T obj = clazz.newInstance(); Schema<T> schema = getSchema(clazz); ProtostuffIOUtil.mergeFrom(data, obj, schema); return obj; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }}

User.java

package tech.mrbcy.mrpc.demo.demo1;import java.util.ArrayList;import java.util.List;public class User { private int userId; private String userName; private String pw; private List<String> addresses = new ArrayList<String>(); public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPw() { return pw; } public void setPw(String pw) { this.pw = pw; } public void addAddress(String address){ addresses.add(address); } @Override public String toString() { return "User [userId=" + userId + ", userName=" + userName + ", pw=" + pw + ", addresses=" + addresses + "]"; }}

Demo1.java

package tech.mrbcy.mrpc.demo.demo1;import org.junit.Test;public class Demo1Test { @Test public void testRpotostuffUtil(){ User user = new User(); user.setUserId(10000); user.setUserName("张三"); user.setPw("123456"); user.addAddress("北京市"); user.addAddress("上海市"); // 序列化 byte[] bytes = ProtostuffUtil.serializer(user); System.out.println("数组长度:" + bytes.length); // 反序列化 User newUser = ProtostuffUtil.deserializer(bytes, User.class); System.out.println(newUser); }}

输出结果:

数组长度:41User [userId=10000, userName=张三, pw=123456, addresses=[北京市, 上海市]]

项目maven的pom.xml代码如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>tech.mrbcy.mrpc</groupId> <artifactId>Mrpc</artifactId> <version>0.0.1</version> <dependencies> <!-- Protostuff --> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.0.8</version> </dependency> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>1.0.8</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build/></project>
上一篇:HashMap

下一篇:图的两种表示和接口

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表