亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MessagePack Java 0.6.X 使用一個消息打包(message-packable)類

發布時間:2020-06-30 10:14:52 來源:網絡 閱讀:229 作者:HoneyMoose 欄目:大數據

使用注解?@Message?來讓你可以序列化你自己類中對象的?public 字段。

本代碼可以在?https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Object.java?中下載到本地后進行編譯測試。

package?com.insight.demo.msgpack;

import?org.junit.Test;
import?org.msgpack.MessagePack;
import?org.msgpack.annotation.Message;
import?org.slf4j.Logger;
import?org.slf4j.LoggerFactory;

import?java.util.UUID;

import?static?org.junit.Assert.assertEquals;

/**
?*?MessagePack6Objects
?*
?*?@author?yhu
?*/
public?class?MessagePack6Object?{
????final?Logger?logger?=?LoggerFactory.getLogger(MessagePack6Object.class);

????/**
?????*?MessageData?Message?Object
?????*/
????@Message?//?Annotation
????public?static?class?MessageData?{
????????//?public?fields?are?serialized.
????????public?String?uuid;
????????public?String?name;
????????public?double?version;
????}


????/**
?????*?Test?MessagePack6Objects
?????*/
????@Test
????public?void?testMessagePack6Objects()?{
????????logger.debug("MessagePack6Objects?for?Objects");

????????String?uuid?=?UUID.randomUUID().toString();

????????//?INIT?OBJ
????????MessageData?src?=?new?MessageData();
????????src.uuid?=?uuid;
????????src.name?=?"MessagePack6";
????????src.version?=?0.6;

????????try?{
????????????MessagePack?msgPack?=?new?MessagePack();

????????????//?Serialization
????????????logger.debug("------?Serialization?------");
????????????byte[]?bytes?=?msgPack.write(src);
????????????logger.debug("Bytes?Array?Length:?[{}]",?bytes.length);

????????????//?Deserialization
????????????logger.debug("------?Deserialization?------");
????????????MessageData?dst?=?msgPack.read(bytes,?MessageData.class);
????????????logger.debug("Check?Object?for?UUID:?[{}]",?dst.uuid);

????????????assertEquals(uuid,?dst.uuid);

????????}?catch?(Exception?ex)?{
????????????logger.error("MessagePack?Serialization?And?Deserialization?error",?ex);
????????}
????}
}

?

如果你希望按照順序序列化多個對象的話,你可以使用??Packer?和??Unpacker?多個對象。

這是因為?MessagePack.write(Object)?和?read(byte[])?實際上每次都調用創建了?Packer?和?Unpacker?對象。

為了使用?Packer?和??Unpacker?對象,請調用?createPacker(OutputStream)?和??createUnpacker(InputStream)

本代碼可以?https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Objects.java?中查看。

?

package?com.insight.demo.msgpack;

import?org.junit.Test;
import?org.msgpack.MessagePack;
import?org.msgpack.annotation.Message;
import?org.msgpack.packer.Packer;
import?org.msgpack.unpacker.Unpacker;
import?org.slf4j.Logger;
import?org.slf4j.LoggerFactory;

import?java.io.ByteArrayInputStream;
import?java.io.ByteArrayOutputStream;
import?java.util.UUID;

import?static?org.junit.Assert.assertEquals;

/**
?*?MessagePack6Objects
?*
?*?@author?yhu
?*/
public?class?MessagePack6Objects?{
????final?Logger?logger?=?LoggerFactory.getLogger(MessagePack6Objects.class);

????/**
?????*?MessageData?Message?Objects
?????*/
????@Message?//?Annotation
????public?static?class?MessageData?{
????????//?public?fields?are?serialized.
????????public?String?uuid;
????????public?String?name;
????????public?double?version;
????}


????/**
?????*?Test?MessagePack6Objects
?????*/
????@Test
????public?void?testMessagePack6Objects()?{
????????logger.debug("MessagePack6Objects?for?Objects");

????????String?uuid?=?UUID.randomUUID().toString();

????????//?INIT?OBJ
????????MessageData?src1?=?new?MessageData();
????????src1.uuid?=?uuid;
????????src1.name?=?"MessagePack6-src1";
????????src1.version?=?0.6;

????????MessageData?src2?=?new?MessageData();
????????src2.uuid?=?uuid;
????????src2.name?=?"MessagePack6-src2";
????????src2.version?=?10.6;

????????MessageData?src3?=?new?MessageData();
????????src3.uuid?=?uuid;
????????src3.name?=?"MessagePack6-src3";
????????src3.version?=?1.6;

????????try?{
????????????MessagePack?msgPack?=?new?MessagePack();

????????????//?Serialization
????????????logger.debug("------?Serialization?------");
????????????ByteArrayOutputStream?out?=?new?ByteArrayOutputStream();
????????????Packer?packer?=?msgPack.createPacker(out);
????????????packer.write(src1);
????????????packer.write(src2);
????????????packer.write(src3);

????????????byte[]?bytes?=?out.toByteArray();
????????????logger.debug("Bytes?Array?Length:?[{}]",?bytes.length);

????????????//?Deserialization
????????????logger.debug("------?Deserialization?------");
????????????ByteArrayInputStream?in?=?new?ByteArrayInputStream(bytes);
????????????Unpacker?unpacker?=?msgPack.createUnpacker(in);

????????????MessageData?dst1?=?unpacker.read(MessageData.class);
????????????MessageData?dst2?=?unpacker.read(MessageData.class);
????????????MessageData?dst3?=?unpacker.read(MessageData.class);

????????????logger.debug("Check?Object?for?UUID:?[{}]",?dst1.uuid);

????????????assertEquals(uuid,?dst1.uuid);

????????}?catch?(Exception?ex)?{
????????????logger.error("MessagePack?Serialization?And?Deserialization?error",?ex);
????????}
????}
}

?

?

https://www.cwiki.us/display/Serialization/QuickStart+For+MessagePack+Java+0.6.X


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

莱芜市| 永修县| 湘乡市| 许昌市| 加查县| 合水县| 江达县| 大埔区| 杂多县| 平昌县| 资溪县| 黄大仙区| 奉节县| 洞头县| 金秀| 商丘市| 阿合奇县| 枝江市| 资中县| 永春县| 都江堰市| 城市| 东乌珠穆沁旗| 广东省| 上杭县| 开鲁县| 永州市| 辽中县| 通许县| 巢湖市| 虹口区| 乐安县| 曲麻莱县| 五河县| 绥芬河市| 土默特右旗| 济阳县| 甘肃省| 德令哈市| 信阳市| 都江堰市|