Protostuff 的Java序列化和反序列化
<dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>1.4.0</version> </dependency>
import java.util.Map; import io.protostuff.LinkedBuffer; import io.protostuff.ProtostuffIOUtil; import io.protostuff.Schema; import io.protostuff.runtime.RuntimeSchema; /** * @author rainyday * @createTime 2018/6/5. */ public class SerializationUtil { private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<>(); private static <T> Schema<T> getSchema(Class<T> cls) { Schema<T> schema = (Schema<T>) cachedSchema.get(cls); if (schema == null) { schema = RuntimeSchema.createFrom(cls); if (schema != null) { cachedSchema.put(cls, schema); } } return schema; } @SuppressWarnings("unchecked") public static <T> byte[] serialize(T obj) { Class<T> cls = (Class<T>) obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema<T> schema = getSchema(cls); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } } public static <T> T deserialize(byte[] data, Class<T> cls) { try { T obj = cls.newInstance(); Schema<T> schema = getSchema(cls); ProtostuffIOUtil.mergeFrom(data, obj, schema); return obj; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } } }

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Ubuntu18.04安装Tensorflow-gpu
写篇blog记录一下配置tensorflow-gpu开发环境 环境版本 系统:Ubuntu 18.04 LTS GPU:GeForce GTX 1050 Mobile Cuda: 9.0 Cudnn: 7.0.5 Tensorflow-gpu:1.8.0 Python:python3.6.5 安装Cuda Toolkit 9.0 Cuda Toolkit 9.0下载链接 我的选择 另外,下面的两个Patch 我都有下载,都是用dpkg装的 去到你下载好的三个deb的文件夹中,在终端执行下面的语句: sudo apt-key add /var/cuda-repo-9-0-local-cublas-performance-update-2/7fa2af80.pub sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb sudo dpkg -i cuda-repo-ubuntu1604-9-0-local-cublas-performance-update_1.0-1_amd64.deb sudo dpkg -i cu...
- 下一篇
C# 桌面软件开发-深入学习 [1]- AY-C#人爱学不学-aaronyang技术分享
原文: C# 桌面软件开发-深入学习 [1]- AY-C#人爱学不学-aaronyang技术分享 曾经我做office,不想依赖别人dll,就使用了 Type.GetTypeFromProgID 可以根据 一个 ID 获得office的操作对象了,当然你也可以获得其他的操作对象,这个id就像一个密码。 ====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。========= 我AY的教程是按照我 高中的思维水平来理解的,如果有问题,还请指出。 写一个控制台或者 WPF程序 新建一个测试用的 类型 C# public class AyClass1 { public string P1 { get; set; } public int P2 { get; set; } public int M1() { return P2 * 2; } public int M2(int t) { return P2 * t; } public void M3() { } } C# Ay...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Hadoop3单机部署,实现最简伪集群
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Windows10,CentOS7,CentOS8安装Nodejs环境
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8编译安装MySQL8.0.19
- CentOS关闭SELinux安全模块
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7