com.faster .jackson.databind. Mapper 在默认的情况下在写出输入后将会关闭输出流(output stream)。

如果你希望序列化多值变量在同一个输出流的情况下,你不希望在输出完一个就关闭输出流,你可以设置  JsonGenerator.Feature.AUTO_CLOSE_TARGET 参数为 False。

本测试方法,可以在 https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java 中找到。

/** * Serialization Not Close output stream */@Testpublic void testMessagePackSerializationNotCloseOutputStream() {    logger.debug("testMessagePackSerializationNotCloseOutputStream");    try {        File tempFile = File.createTempFile("messagepack-", "-cwiki.us");        OutputStream out = new FileOutputStream(tempFile);         Mapper  Mapper = new  Mapper(new MessagePackFactory());         Mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);         Mapper.writeValue(out, 1);         Mapper.writeValue(out, "two");         Mapper.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);    }}

https://www.cwiki.us/display/Serialization/MessagePack+Jackson+Dataformat

收藏 打印