真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Kryo序列化及反序列化用法示例

Kryo 是一個快速高效的 Java 對象圖形序列化框架,主要特點(diǎn)是性能、高效和易用。該項目用來序列化對象到文件、數(shù)據(jù)庫或者網(wǎng)絡(luò)。

我們提供的服務(wù)有:成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、烏魯木齊ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的烏魯木齊網(wǎng)站制作公司

    代碼地址:https://github.com/EsotericSoftware/kryo

    樣例代碼地址:https://github.com/nettm/public

    Kryo的序列化及反序列速度很快,據(jù)說很多大公司都在用。我在把對象序列化都轉(zhuǎn)換成了字符串形式,是為了把對象存儲到緩存中。我們?nèi)粘m椖恐惺褂玫臄?shù)據(jù)形式包括對象、List、Set和Map,因此主要把這幾種類型的數(shù)據(jù)進(jìn)行了序列化及反序列化,支持對象中包含List、Set和Map。

首先在項目的pom文件中引入:


  com.esotericsoftware
  kryo-shaded
  3.0.3

代碼KryoTest:

package com.nettm.serializable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.codec.binary.Base64;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.JavaSerializer;
import com.esotericsoftware.kryo.serializers.MapSerializer;
public class KryoTest {
  private long time;
  @BeforeTest
  public void beforeTest() {
    time = System.currentTimeMillis();
  }
  @AfterTest
  public void afterTest() {
    System.out.println(System.currentTimeMillis() - time);
  }
  @Test(invocationCount = 1, threadPoolSize = 1)
  public void testObject() {
    CustomItemDto val = new CustomItemDto();
    val.setId(Long.parseLong(String.valueOf(1)));
    val.setItemCode("");
    val.setItemDespositPrice(32.45);
    val.setItemMemo(null);
    val.setItemName("張金");
    val.setItemPrice(89.02);
    val.setSort(10);
    String a = serializationObject(val);
    CustomItemDto newValue = deserializationObject(a, CustomItemDto.class);
    Assert.assertEquals(val.getId(), newValue.getId());
  }
  @Test(invocationCount = 1, threadPoolSize = 1)
  public void testList() {
    List lst = new ArrayList();
    for (int i = 0; i < 10; i++) {
      CustomItemDto val = new CustomItemDto();
      val.setId(Long.parseLong(String.valueOf(i)));
      val.setItemCode("");
      val.setItemDespositPrice(32.45);
      val.setItemMemo(null);
      val.setItemName("張金");
      val.setItemPrice(89.02);
      val.setSort(10);
      lst.add(val);
    }
    String a = serializationList(lst, CustomItemDto.class);
    List newValue = deserializationList(a,
        CustomItemDto.class);
    Assert.assertEquals(lst.size(), newValue.size());
  }
  @Test(invocationCount = 1, threadPoolSize = 1)
  public void testBean() {
    List lst = new ArrayList();
    for (int j = 0; j < 10; j++) {
      CustomCategoryDto dto = new CustomCategoryDto();
      dto.setCategoryCode("ABCD_001");
      dto.setCategoryName("呼吸系統(tǒng)");
      for (int i = 0; i < 10; i++) {
        CustomItemDto val = new CustomItemDto();
        val.setId(Long.parseLong(String.valueOf(i)));
        val.setItemCode("");
        val.setItemDespositPrice(32.45);
        val.setItemMemo(null);
        val.setItemName("張金");
        val.setItemPrice(89.02);
        val.setSort(10);
        dto.getCustomItemList().add(val);
      }
      for (int i = 0; i < 10; i++) {
        CustomItemDto val = new CustomItemDto();
        val.setId(Long.parseLong(String.valueOf(i)));
        val.setItemCode("");
        val.setItemDespositPrice(32.45);
        val.setItemMemo(null);
        val.setItemName("張金");
        val.setItemPrice(89.02);
        val.setSort(10);
        dto.getCustomItemSet().add(val);
      }
      for (int i = 0; i < 10; i++) {
        CustomItemDto val = new CustomItemDto();
        val.setId(Long.parseLong(String.valueOf(i)));
        val.setItemCode("");
        val.setItemDespositPrice(32.45);
        val.setItemMemo(null);
        val.setItemName("張金");
        val.setItemPrice(89.02);
        val.setSort(10);
        dto.getCustomItemMap().put(String.valueOf(i), val);
      }
      lst.add(dto);
    }
    String a = serializationList(lst, CustomCategoryDto.class);
    List newValue = deserializationList(a,
        CustomCategoryDto.class);
    Assert.assertEquals(lst.size(), newValue.size());
  }
  @Test(invocationCount = 1, threadPoolSize = 1)
  public void testMap() {
    Map map = new HashMap();
    for (int i = 0; i < 10; i++) {
      CustomItemDto val = new CustomItemDto();
      val.setId(Long.parseLong(String.valueOf(i)));
      val.setItemCode("");
      val.setItemDespositPrice(32.45);
      val.setItemMemo(null);
      val.setItemName("張金");
      val.setItemPrice(89.02);
      val.setSort(10);
      map.put(new ObjectId().toString(), val);
    }
    String a = serializationMap(map, CustomItemDto.class);
    Map newValue = deserializationMap(a,
        CustomItemDto.class);
    Assert.assertEquals(map.size(), newValue.size());
  }
  @Test(invocationCount = 1, threadPoolSize = 1)
  public void testSet() {
    Set set = new HashSet();
    for (int i = 0; i < 10; i++) {
      CustomItemDto val = new CustomItemDto();
      val.setId(Long.parseLong(String.valueOf(i)));
      val.setItemCode("");
      val.setItemDespositPrice(32.45);
      val.setItemMemo(null);
      val.setItemName("金星");
      val.setItemPrice(89.02);
      val.setSort(10);
      set.add(val);
    }
    String a = serializationSet(set, CustomItemDto.class);
    Set newValue = deserializationSet(a, CustomItemDto.class);
    Assert.assertEquals(set.size(), newValue.size());
  }
  private  String serializationObject(T obj) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.register(obj.getClass(), new JavaSerializer());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeClassAndObject(output, obj);
    output.flush();
    output.close();
    byte[] b = baos.toByteArray();
    try {
      baos.flush();
      baos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return new String(new Base64().encode(b));
  }
  @SuppressWarnings("unchecked")
  private  T deserializationObject(String obj,
      Class clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.register(clazz, new JavaSerializer());
    ByteArrayInputStream bais = new ByteArrayInputStream(
        new Base64().decode(obj));
    Input input = new Input(bais);
    return (T) kryo.readClassAndObject(input);
  }
  private  String serializationList(List obj,
      Class clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.setRegistrationRequired(true);
    CollectionSerializer serializer = new CollectionSerializer();
    serializer.setElementClass(clazz, new JavaSerializer());
    serializer.setElementsCanBeNull(false);
    kryo.register(clazz, new JavaSerializer());
    kryo.register(ArrayList.class, serializer);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeObject(output, obj);
    output.flush();
    output.close();
    byte[] b = baos.toByteArray();
    try {
      baos.flush();
      baos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return new String(new Base64().encode(b));
  }
  @SuppressWarnings("unchecked")
  private  List deserializationList(String obj,
      Class clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.setRegistrationRequired(true);
    CollectionSerializer serializer = new CollectionSerializer();
    serializer.setElementClass(clazz, new JavaSerializer());
    serializer.setElementsCanBeNull(false);
    kryo.register(clazz, new JavaSerializer());
    kryo.register(ArrayList.class, serializer);
    ByteArrayInputStream bais = new ByteArrayInputStream(
        new Base64().decode(obj));
    Input input = new Input(bais);
    return (List) kryo.readObject(input, ArrayList.class, serializer);
  }
  private  String serializationMap(
      Map obj, Class clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.setRegistrationRequired(true);
    MapSerializer serializer = new MapSerializer();
    serializer.setKeyClass(String.class, new JavaSerializer());
    serializer.setKeysCanBeNull(false);
    serializer.setValueClass(clazz, new JavaSerializer());
    serializer.setValuesCanBeNull(true);
    kryo.register(clazz, new JavaSerializer());
    kryo.register(HashMap.class, serializer);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeObject(output, obj);
    output.flush();
    output.close();
    byte[] b = baos.toByteArray();
    try {
      baos.flush();
      baos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return new String(new Base64().encode(b));
  }
  @SuppressWarnings("unchecked")
  private  Map deserializationMap(
      String obj, Class clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.setRegistrationRequired(true);
    MapSerializer serializer = new MapSerializer();
    serializer.setKeyClass(String.class, new JavaSerializer());
    serializer.setKeysCanBeNull(false);
    serializer.setValueClass(clazz, new JavaSerializer());
    serializer.setValuesCanBeNull(true);
    kryo.register(clazz, new JavaSerializer());
    kryo.register(HashMap.class, serializer);
    ByteArrayInputStream bais = new ByteArrayInputStream(
        new Base64().decode(obj));
    Input input = new Input(bais);
    return (Map) kryo.readObject(input, HashMap.class,
        serializer);
  }
  public static  String serializationSet(Set obj,
      Class clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.setRegistrationRequired(true);
    CollectionSerializer serializer = new CollectionSerializer();
    serializer.setElementClass(clazz, new JavaSerializer());
    serializer.setElementsCanBeNull(false);
    kryo.register(clazz, new JavaSerializer());
    kryo.register(HashSet.class, serializer);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeObject(output, obj);
    output.flush();
    output.close();
    byte[] b = baos.toByteArray();
    try {
      baos.flush();
      baos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return new String(new Base64().encode(b));
  }
  @SuppressWarnings("unchecked")
  public static  Set deserializationSet(
      String obj, Class clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.setRegistrationRequired(true);
    CollectionSerializer serializer = new CollectionSerializer();
    serializer.setElementClass(clazz, new JavaSerializer());
    serializer.setElementsCanBeNull(false);
    kryo.register(clazz, new JavaSerializer());
    kryo.register(HashSet.class, serializer);
    ByteArrayInputStream bais = new ByteArrayInputStream(
        new Base64().decode(obj));
    Input input = new Input(bais);
    return (Set) kryo.readObject(input, HashSet.class, serializer);
  }
}

總結(jié)

以上就是本文關(guān)于Kryo序列化及反序列化用法示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以參閱:java原生序列化和Kryo序列化性能實例對比分析 、 Kryo框架使用方法代碼示例等,有什么問題可以隨時留言,小編一定竭盡所能,答您所問。


網(wǎng)頁名稱:Kryo序列化及反序列化用法示例
當(dāng)前路徑:http://weahome.cn/article/ijheos.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部