Optimize anyxml output 44/84344/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Sep 2019 15:33:59 +0000 (17:33 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Sep 2019 15:34:34 +0000 (17:34 +0200)
We do not need to instantiate a new factory every time we perform
encoding, keep the factory as a shared constant.

Change-Id: Ideff56076a1516784cd82f67438e5b740c4d3eeb
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractLithiumDataOutput.java

index e8b7547..2320775 100644 (file)
@@ -28,7 +28,6 @@ import java.util.Optional;
 import java.util.Set;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.TransformerFactoryConfigurationError;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import org.eclipse.jdt.annotation.NonNull;
@@ -56,6 +55,7 @@ import org.slf4j.LoggerFactory;
  */
 abstract class AbstractLithiumDataOutput extends AbstractNormalizedNodeDataOutput {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractLithiumDataOutput.class);
+    private static final TransformerFactory TF = TransformerFactory.newInstance();
     private static final ImmutableMap<Class<?>, Byte> KNOWN_TYPES = ImmutableMap.<Class<?>, Byte>builder()
             .put(String.class, ValueTypes.STRING_TYPE)
             .put(Byte.class, ValueTypes.BYTE_TYPE)
@@ -191,13 +191,13 @@ abstract class AbstractLithiumDataOutput extends AbstractNormalizedNodeDataOutpu
 
     @Override
     public final void domSourceValue(final DOMSource value) throws IOException {
+        final StringWriter writer = new StringWriter();
         try {
-            StreamResult xmlOutput = new StreamResult(new StringWriter());
-            TransformerFactory.newInstance().newTransformer().transform(value, xmlOutput);
-            writeObject(xmlOutput.getWriter().toString());
-        } catch (TransformerException | TransformerFactoryConfigurationError e) {
+            TF.newTransformer().transform(value, new StreamResult(writer));
+        } catch (TransformerException e) {
             throw new IOException("Error writing anyXml", e);
         }
+        writeObject(writer.toString());
     }
 
     @Override

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.