MessagePack Java Jackson 在不关闭输出流(output stream)的情况下序列化多变量
com.fasterxml.jackson.databind.ObjectMapper
在默认的情况下在写出输入后将会关闭输出流(output stream)。
如果你希望序列化多值变量在同一个输出流的情况下,你不希望在输出完一个就关闭输出流,你可以设置 JsonGenerator.Feature.AUTO_CLOSE_TARGET
参数为 False
。
/** * 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