MessagePack Java Jackson without closing the output stream (output stream) sequence of multivariate

com.fasterxml.jackson.databind.ObjectMapper In the case of default input will turn off after writing the output stream (output stream).

If you want a sequence of multi-value variable in a case where the same output stream, you do not want the output of a finished output stream is closed, you can set   JsonGenerator.Feature.AUTO_CLOSE_TARGET parameters False.

This test method may  https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java  in turn up.

 

/**
 * 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);
    }
}

 

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

Guess you like

Origin www.cnblogs.com/huyuchengus/p/11330359.html