您好,登錄后才能下訂單哦!
類?Packer
/Unpacker
?允許序列化和反序列化多種類型的變量,如后續程序所示。這個類啟用序列化和反序列化多種類型的變量和序列化主要類型變量以及包裝類,String
?對象,byte[]
?對象,?ByteBuffer
?對象等的方法相似。
如上面提示的,你可以序列化和反序列化你自己的對象,前提是你自己的對象需要使用?@Message
?注解。
package?com.insight.demo.msgpack; import?org.junit.Test; import?org.msgpack.MessagePack; 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.math.BigInteger; import?java.nio.ByteBuffer; /** ?*?MessagePack6Types ?* ?*?@author?yhu ?*/ public?class?MessagePack6Types?{ ????final?Logger?logger?=?LoggerFactory.getLogger(MessagePack6Types.class); ????/** ?????*?Test?MessagePack6Types ?????*/ ????@Test ????public?void?testMessagePack6Types()?{ ????????logger.debug("testMessagePack6Types?for?Types"); ????????MessagePack?msgpack?=?new?MessagePack(); ????????try?{ ????????????// ????????????//?Serialization ????????????// ????????????ByteArrayOutputStream?out?=?new?ByteArrayOutputStream(); ????????????Packer?packer?=?msgpack.createPacker(out); ????????????//?Serialize?values?of?primitive?types ????????????packer.write(true);?//?boolean?value ????????????packer.write(10);?//?int?value ????????????packer.write(10.5);?//?double?value ????????????//?Serialize?objects?of?primitive?wrapper?types ????????????packer.write(Boolean.TRUE); ????????????packer.write(new?Integer(10)); ????????????packer.write(new?Double(10.5)); ????????????//?Serialize?various?types?of?arrays ????????????packer.write(new?int[]{1,?2,?3,?4}); ????????????packer.write(new?Double[]{10.5,?20.5}); ????????????packer.write(new?String[]{"msg",?"pack",?"for",?"java"}); ????????????packer.write(new?byte[]{0x30,?0x31,?0x32});?//?byte?array ????????????//?Serialize?various?types?of?other?reference?values ????????????packer.write("MessagePack");?//?String?object ????????????packer.write(ByteBuffer.wrap(new?byte[]{0x30,?0x31,?0x32}));?//?ByteBuffer?object ????????????packer.write(BigInteger.ONE);?//?BigInteger?object ????????????// ????????????//?Deserialization ????????????// ????????????byte[]?bytes?=?out.toByteArray(); ????????????ByteArrayInputStream?in?=?new?ByteArrayInputStream(bytes); ????????????Unpacker?unpacker?=?msgpack.createUnpacker(in); ????????????//?to?primitive?values ????????????boolean?b?=?unpacker.readBoolean();?//?boolean?value ????????????int?i?=?unpacker.readInt();?//?int?value ????????????double?d?=?unpacker.readDouble();?//?double?value ????????????//?to?primitive?wrapper?value ????????????Boolean?wb?=?unpacker.read(Boolean.class); ????????????Integer?wi?=?unpacker.read(Integer.class); ????????????Double?wd?=?unpacker.read(Double.class); ????????????//?to?arrays ????????????int[]?ia?=?unpacker.read(int[].class); ????????????Double[]?da?=?unpacker.read(Double[].class); ????????????String[]?sa?=?unpacker.read(String[].class); ????????????byte[]?ba?=?unpacker.read(byte[].class); ????????????//?to?String?object,?ByteBuffer?object,?BigInteger?object,?List?object?and?Map?object ????????????String?ws?=?unpacker.read(String.class); ????????????ByteBuffer?buf?=?unpacker.read(ByteBuffer.class); ????????????BigInteger?bi?=?unpacker.read(BigInteger.class); ????????}?catch?(Exception?ex)?{ ????????????logger.error("MessagePack?Serialization?And?Deserialization?error",?ex); ????????} ????} }
方法?Packer#write()
?允許序列化多種類型的數據。
類?Unpacker
?針對反序列化二進制數據為主要變量,提供了一個反序列化方法。例如,你希望將二進制數據反序列化為?boolean
?(或者?int
) 數據類型,你可以使用?Unpacker
?中的?readBoolean
?(或者?readInt
) 方法。
Unpacker
?同時也為參考變量提供了一個讀取的方法。這個方法允許為一個參考變量從二進制數據中進行反序列化。參考變量的定義為你將類型指定為一個參數。例如,你希望反序列化二進制數據到?String
?(或者?byte[]
) 對象,你必須在調用?read(String.class)
?(或者?read(byte[].class)
) 方法的時候定義描述。
?
https://www.cwiki.us/display/Serialization/QuickStart+For+MessagePack+Java+0.6.X
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。