Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

MessagePack Java Jackson without closing the input stream (input stream)

Shulou Source: shulou.com Published: 2022-06-03 01:27:53 09月13日 Update

Com.fasterxml.jackson.databind.ObjectMapper will close the input stream by default when reading input stream variables.

If you do not want to close the input stream, you can set the JsonParser.Feature.AUTO_CLOSE_SOURCE parameter to false.

This test method can be found in https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/MessagePackSerializer.java.

/ * SerializationNotClose input stream * / @ Testpublic void testMessagePackSerializationNotCloseInputStream () {logger.debug ("testMessagePackSerializationNotCloseInputStream"); try {File tempFile = File.createTempFile ("messagepack-", "- cwiki.us"); MessagePacker packer = MessagePack.newDefaultPacker (new FileOutputStream (tempFile)); packer.packInt (42); packer.packString ("Hello"); packer.close (); FileInputStream in = new FileInputStream (tempFile) ObjectMapper objectMapper = new ObjectMapper (new MessagePackFactory ()); objectMapper.configure (JsonParser.Feature.AUTO_CLOSE_SOURCE, false); System.out.println (objectMapper.readValue (in, Integer.class)); System.out.println (objectMapper.readValue (in, String.class)); in.close (); tempFile.deleteOnExit ();} catch (IOException ex) {logger.error ("Serialize Error", ex);}}

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

Tags: Input parameter variable method time test situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Shulou Tech Info Docker NVidia