From faa9f2b26d0c3ede921372fe284935d1a1947a99 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 9 Aug 2019 16:57:01 -0400 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E5=8F=98=E9=87=8F=E4=B8=8D?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E8=BE=93=E5=87=BA=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/serialize/MessagePackSerializer.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java b/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java index 556664b..71ccdb1 100644 --- a/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java +++ b/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java @@ -1,5 +1,6 @@ package com.insight.demo.serialize; +import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -7,11 +8,13 @@ import com.insight.demo.serialize.model.msgpack.MessageData; import com.insight.demo.utils.MockDataUtils; import org.junit.Assert; import org.junit.Test; +import org.msgpack.core.MessagePack; +import org.msgpack.core.MessageUnpacker; import org.msgpack.jackson.dataformat.MessagePackFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; +import java.io.*; import java.util.*; import static org.junit.Assert.assertEquals; @@ -141,6 +144,36 @@ public class MessagePackSerializer { } + /** + * Serialization Not Close output stream + */ + @Test + public void testMessagePackSerializationNotCloseOutputStream() { + logger.debug("testMessagePackSerializationNotCloseOutputStream"); + + try { + File tempFile = File.createTempFile("messagepack-", "-cwiki.us"); + + OutputStream out = new FileOutputStream(tempFile); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); + objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); + + objectMapper.writeValue(out, 1); + objectMapper.writeValue(out, "two"); + objectMapper.writeValue(out, 3.14); + out.close(); + + MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new FileInputStream(tempFile)); + System.out.println(unpacker.unpackInt()); // => 1 + System.out.println(unpacker.unpackString()); // => two + System.out.println(unpacker.unpackFloat()); // => 3.14 + + tempFile.deleteOnExit(); + } catch (IOException ex) { + logger.error("Serialize Error", ex); + } + } + @Test public void testMessagePackSerialization() { ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());