fst,与kryo类似是apache组织的一个开源项目,完全兼容JDK序列化协议的系列化框架,序列化速度大概是JDK的4-10倍,大小是JDK大小的1/3左右
官方文档:
https://github.com/RuedigerMoeller/fast-serialization/wiki/Serialization
pom:
<dependency>
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>
<version>2.56</version>
</dependency>
package cn.witsky.utils;
import org.nustaq.serialization.FSTConfiguration;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;
import java.io.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FstUtils {
static FSTConfiguration conf = FSTConfiguration.createStructConfiguration();//.createDefaultConfiguration();
private static ThreadLocal<FSTConfiguration> confs = new ThreadLocal(){
public FSTConfiguration initialValue() {
return FSTConfiguration.createDefaultConfiguration();
}
};
private static FSTConfiguration getFST(){
return confs.get();
}
/**
* 将指定对象序列化为byte数组
* @param obj
* @return
*/
public static <T> byte[] serialize(T obj) {
return conf.asByteArray(obj);
}
/**
* 将byte数组反序列化为指定对象
* @param sec
* @return
*/
public static <T> T deserialize(byte[] sec) {
return (T)conf.asObject(sec);
}
/**
* 将指定对象序列化为byte数组
* @param obj
* @return
*/
public static <T> byte[] serialize2(T obj) {
return getFST().asByteArray(obj);
}
/**
* 将byte数组反序列化为指定对象
* @param sec
* @return
*/
public static <T> T deserialize2(byte[] sec) {
return (T)getFST().asObject(sec);
}
/**
* 将指定对象序列化存储在文件中
* @param path
* @param obj
* @param <T>
*/
public static <T> void serializeT(T obj, String path){
try{
FSTObjectOutput out = conf.getObjectOutput(new FileOutputStream(new File(path)));
out.writeObject(obj);
out.flush();
}catch (Exception e){
throw new RuntimeException("FstUtils serializeT exception");
}
}
/**
* 将存储在文件中的序列化反序列化为指定对象
* @param path
* @param <T>
* @return
*/
public static <T> T deserializeT(String path){
try {
FSTObjectInput in = conf.getObjectInput(new FileInputStream(new File(path)));
return (T) in.readObject();
}catch (Exception e){
throw new RuntimeException("FstUtils deserializeT exception");
}
}
/**
* 将指定对象序列化存储在文件中
* @param obj
* @param <T>
* @return
* @throws IOException
*/
public static <T> void serializeT2(T obj, String path) throws IOException {
try{
// ObjectOutputStream 对象输出流,将对象存储到文件中,完成对T对象的序列化操作
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(new File(path)));
oo.writeObject(obj);
oo.close();
}catch (Exception e){
throw new RuntimeException("FstUtils serializeT2 exception");
}
}
/**
* 将存储在文件中的序列化反序列化为指定对象
* @param <T>
* @return
* @throws Exception
*/
public static <T> T deserializeT2(String path){
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(path)));
return (T)ois.readObject();
}catch (Exception e){
throw new RuntimeException("FstUtils deserializeT2 exception");
}
}
/**
* 将List对象序列化为byte数组
* @param obj
* @return
*/
public static <T> byte[] serializeList(List<T> obj) {
return getFST().asByteArray(obj);
}
/**
* 将byte数组反序列化为List对象
* @param sec
* @return
*/
public static <T> List<T> deserializeList(byte[] sec) {
return (List<T>)getFST().asObject(sec);
}
/**
* 将Map对象序列化为byte数组
* @param obj
* @return
*/
public static <K, V> byte[] serializeMap(Map<K, V> obj) {
return getFST().asByteArray(obj);
}
/**
* 将byte数组反序列化为Map对象
* @param sec
* @return
*/
public static <K, V> Map<K, V> deserializeMap(byte[] sec) {
return (Map<K, V>)getFST().asObject(sec);
}
public static void main(String[] args) throws Exception {
/*Test obj = new Test();
obj.setA("gacl");
obj.setB("男");
byte[] ser1 = serialize(obj);
Test t1 = deserialize(ser1);
System.out.println(MessageFormat.format("name={0},sex={1}", t1.getA(), t1.getB()));
byte[] ser2 = serialize2(obj);
Test t2 = deserialize2(ser2);
System.out.println(MessageFormat.format("name={0},sex={1}", t2.getA(), t2.getB()));
serializeT(obj, "E:/Person.txt");//序列化对象
Test tt1 = deserializeT("E:/Person.txt");//反序列对象
System.out.println(MessageFormat.format("name={0},sex={1}", tt1.getA(), tt1.getB()));
serializeT2(obj, "E:/Person2.txt");//序列化对象
Test tt2 = deserializeT2("E:/Person2.txt");//反序列对象
System.out.println(MessageFormat.format("name={0},sex={1}", tt2.getA(), tt2.getB()));*/
List<Test> list = new ArrayList<Test>();
for (int i = 0; i <= 5; i++){
Test obj = new Test();
obj.setA("gacl" + i);
obj.setB("男" + i);
list.add(obj);
}
byte[] ser3 = serializeList(list);
List<Test> lt1 = deserializeList(ser3);
for (Test t: lt1) {
System.out.println(MessageFormat.format("name={0},sex={1}", t.getA(), t.getB()));
}
Map<String, Test> map = new HashMap<String, Test>();
for (int i = 0; i <= 5; i++){
Test obj = new Test();
obj.setA("gacl" + i);
obj.setB("男" + i);
map.put("map" + i, obj);
}
byte[] ser4 = serializeMap(map);
Map<String, Test> mt1 = deserializeMap(ser4);
System.out.println(mt1.toString());
}
public static class Test implements Serializable{
private String a;
private String b;
private int c;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
@Override
public String toString() {
return "Test{" +
"a='" + a + '\'' +
", b='" + b + '\'' +
", c=" + c +
'}';
}
}
}