Java利用protostuff实现高效序列化
2017-01-16 10:07
369 查看
pom内容:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-api</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-collectionschema</artifactId>
<version>1.0.10</version>
</dependency>
TIP:使用时需要注意的是,那个bean必须要含有无参构造,并且不能是接口或者抽象类(如使用toBean方法时,不能传Runnable这个接口的class的进来,而是应该传自己实际实现了Runnable接口的类的class,并且还必须有无参的构造函数)
示例:Runnable job = SerializingUtil.readBeanFromFile(jobFile, CloseCompetitionJob.class);
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
public class SerializingUtil {
public static <T> byte[] toByte(T source, Class<T> targetClass) {
RuntimeSchema<T> schema = RuntimeSchema.createFrom(targetClass);
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
byte[] protostuff = null;
try {
protostuff = ProtostuffIOUtil.toByteArray(source, schema, buffer);
return protostuff;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
buffer.clear();
}
return null;
}
public static <T> T toBean(byte[] source, Class<T> targetClass) throws InstantiationException, IllegalAccessException {
RuntimeSchema<T> schema = RuntimeSchema.createFrom(targetClass);
System.out.println(targetClass);
T newInstance = targetClass.newInstance();
ProtostuffIOUtil.mergeFrom(source, newInstance, schema);
return newInstance;
}
public static <T> void writeBeanToFile(T source, Class<T> targetClass, File targetFile) throws IOException {
byte[] classByte = toByte(source, targetClass);
FileUtils.writeByteArrayToFile(targetFile, classByte);
}
public static <T> T readBeanFromFile(File sourceFile, Class<T> targetClass) throws InstantiationException, IllegalAccessException, IOException {
byte[] source = FileUtils.readFileToByteArray(sourceFile);
return toBean(source, targetClass);
}
}
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-api</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-collectionschema</artifactId>
<version>1.0.10</version>
</dependency>
TIP:使用时需要注意的是,那个bean必须要含有无参构造,并且不能是接口或者抽象类(如使用toBean方法时,不能传Runnable这个接口的class的进来,而是应该传自己实际实现了Runnable接口的类的class,并且还必须有无参的构造函数)
示例:Runnable job = SerializingUtil.readBeanFromFile(jobFile, CloseCompetitionJob.class);
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
public class SerializingUtil {
public static <T> byte[] toByte(T source, Class<T> targetClass) {
RuntimeSchema<T> schema = RuntimeSchema.createFrom(targetClass);
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
byte[] protostuff = null;
try {
protostuff = ProtostuffIOUtil.toByteArray(source, schema, buffer);
return protostuff;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
buffer.clear();
}
return null;
}
public static <T> T toBean(byte[] source, Class<T> targetClass) throws InstantiationException, IllegalAccessException {
RuntimeSchema<T> schema = RuntimeSchema.createFrom(targetClass);
System.out.println(targetClass);
T newInstance = targetClass.newInstance();
ProtostuffIOUtil.mergeFrom(source, newInstance, schema);
return newInstance;
}
public static <T> void writeBeanToFile(T source, Class<T> targetClass, File targetFile) throws IOException {
byte[] classByte = toByte(source, targetClass);
FileUtils.writeByteArrayToFile(targetFile, classByte);
}
public static <T> T readBeanFromFile(File sourceFile, Class<T> targetClass) throws InstantiationException, IllegalAccessException, IOException {
byte[] source = FileUtils.readFileToByteArray(sourceFile);
return toBean(source, targetClass);
}
}
相关文章推荐
- Protostuff一键序列化工具、Protobuf JAVA实现
- java序列化/反序列化之xml、protobuf、protostuff 的比较与使用例子
- 序列化框架性能对比(kryo、hessian、java、protostuff)
- Java利用序列化实现对象的深拷贝
- google protobuf 升级版 com.dyuproject.protostuff java 序列化
- 在java中使用kryo框架来实现高效序列化与反序列化
- 序列化框架的使用及性能对比Kryo、Hessian、Protostuff、java原生
- Java序列化技术性能分析(JDK原生与Protostuff)
- Java 深拷贝和浅拷贝 利用序列化实现深拷贝
- java基础-高效利用SimpleDateFormat实现日期的转换
- 序列化框架性能对比(kryo、hessian、java、protostuff)
- Java利用序列化实现对象深度clone的方法
- 序列化框架性能对比(kryo、hessian、java、protostuff)
- Java 利用 Externalizable 实现序列化
- 【Java.ThirdParty】序列化框架性能对比(kryo、hessian、java、protostuff)
- Java反序列化漏洞之weblogic本地利用实现篇
- java序列化/反序列化之xstream、protobuf、protostuff 的比较与使用例子
- java序列化框架性能对比(kryo,hessian,Protostuff)
- 比较protoStuff和fastjson以及实现Serializable接口序列化的效率问题
- java序列化/反序列化之xstream、protobuf、protostuff 的比较与使用例子