Simplify data encoding in AbstractNotificationsData 87/53187/1
authorRobert Varga <rovarga@cisco.com>
Sat, 11 Mar 2017 22:49:39 +0000 (23:49 +0100)
committerRobert Varga <rovarga@cisco.com>
Sat, 11 Mar 2017 22:49:39 +0000 (23:49 +0100)
Rather than performing a two-step conversion via a writer,
output stream directly into bytes and then convert the result
into a String via StandardCharsets.UTF_8.

Change-Id: I010c1f46d2a2f78b619cf7af03c565ec68b6b457
Signed-off-by: Robert Varga <rovarga@cisco.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/streams/listeners/AbstractNotificationsData.java

index f9cefcd0de789d4a8d65486185ac9c11e17c71c2..3b60f677902151521a512eea58f961652379e294 100644 (file)
@@ -9,8 +9,6 @@ package org.opendaylight.netconf.sal.streams.listeners;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
 import java.util.Date;
 import javax.xml.stream.XMLOutputFactory;
@@ -181,22 +179,23 @@ abstract class AbstractNotificationsData {
      * @return - string from {@link Document}
      */
     protected String transformDoc(final Document doc) {
+        final ByteArrayOutputStream out = new ByteArrayOutputStream();
+
         try {
-            final ByteArrayOutputStream out = new ByteArrayOutputStream();
             final Transformer transformer = ListenersConstants.FACTORY.newTransformer();
             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
             transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
-            transformer.transform(new DOMSource(doc),
-                    new StreamResult(new OutputStreamWriter(out, StandardCharsets.UTF_8)));
-            final byte[] charData = out.toByteArray();
-            return new String(charData, "UTF-8");
-        } catch (TransformerException | UnsupportedEncodingException e) {
+            transformer.transform(new DOMSource(doc), new StreamResult(out));
+        } catch (TransformerException e) {
+            // FIXME: this should raise an exception
             final String msg = "Error during transformation of Document into String";
             LOG.error(msg, e);
             return msg;
         }
+
+        return new String(out.toByteArray(), StandardCharsets.UTF_8);
     }
 }