You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2020/01/22 13:06:34 UTC

svn commit: r1873032 [1/2] - in /santuario/xml-security-java/trunk/src: main/java/org/apache/jcp/xml/dsig/internal/dom/ main/java/org/apache/xml/security/c14n/ main/java/org/apache/xml/security/c14n/implementations/ main/java/org/apache/xml/security/en...

Author: coheigea
Date: Wed Jan 22 13:06:34 2020
New Revision: 1873032

URL: http://svn.apache.org/viewvc?rev=1873032&view=rev
Log:
SANTUARIO-521 - Changed canonicalizers to write to the supplied outputstream only, and not return bytes

Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/ApacheCanonicalizer.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerPhysical.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/SignedInfo.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11_WithComments.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NExclusive.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NWithComments.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer11Test.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer20010315ExclusiveTest.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer20010315Test.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/MockCanonicalizationMethod.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Santuario191Test.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Santuario273Test.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/transforms/EmptyNamespaceTest.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/ApacheCanonicalizer.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/ApacheCanonicalizer.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/ApacheCanonicalizer.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/jcp/xml/dsig/internal/dom/ApacheCanonicalizer.java Wed Jan 22 13:06:34 2020
@@ -120,12 +120,8 @@ public abstract class ApacheCanonicalize
             }
         }
 
-        if (os != null) {
-            canonicalizer.setWriter(os);
-        } else {
-            canonicalizer.setWriter(new ByteArrayOutputStream());
-        }
-
+        boolean isByteArrayOutputStream = os == null;
+        OutputStream writer = isByteArrayOutputStream ? new ByteArrayOutputStream() : os;
         try {
             Set<Node> nodeSet = null;
             if (data instanceof ApacheData) {
@@ -133,31 +129,26 @@ public abstract class ApacheCanonicalize
                     ((ApacheData)data).getXMLSignatureInput();
                 if (in.isElement()) {
                     if (inclusiveNamespaces != null) {
-                        return new OctetStreamData(new ByteArrayInputStream
-                            (canonicalizer.canonicalizeSubtree
-                                (in.getSubNode(), inclusiveNamespaces)));
+                        canonicalizer.canonicalizeSubtree(in.getSubNode(), inclusiveNamespaces, writer);
+                        return new OctetStreamData(new ByteArrayInputStream(getC14nBytes(writer, isByteArrayOutputStream)));
                     } else {
-                        return new OctetStreamData(new ByteArrayInputStream
-                            (canonicalizer.canonicalizeSubtree
-                                (in.getSubNode())));
+                        canonicalizer.canonicalizeSubtree(in.getSubNode(), writer);
+                        return new OctetStreamData(new ByteArrayInputStream(getC14nBytes(writer, isByteArrayOutputStream)));
                     }
                 } else if (in.isNodeSet()) {
                     nodeSet = in.getNodeSet();
                 } else {
-                    return new OctetStreamData(new ByteArrayInputStream(
-                        canonicalizer.canonicalize(
-                            Utils.readBytesFromStream(in.getOctetStream()))));
+                    canonicalizer.canonicalize(Utils.readBytesFromStream(in.getOctetStream()), writer);
+                    return new OctetStreamData(new ByteArrayInputStream(getC14nBytes(writer, isByteArrayOutputStream)));
                 }
             } else if (data instanceof DOMSubTreeData) {
                 DOMSubTreeData subTree = (DOMSubTreeData)data;
                 if (inclusiveNamespaces != null) {
-                    return new OctetStreamData(new ByteArrayInputStream
-                        (canonicalizer.canonicalizeSubtree
-                         (subTree.getRoot(), inclusiveNamespaces)));
+                    canonicalizer.canonicalizeSubtree(subTree.getRoot(), inclusiveNamespaces, writer);
+                    return new OctetStreamData(new ByteArrayInputStream(getC14nBytes(writer, isByteArrayOutputStream)));
                 } else {
-                    return new OctetStreamData(new ByteArrayInputStream
-                        (canonicalizer.canonicalizeSubtree
-                         (subTree.getRoot())));
+                    canonicalizer.canonicalizeSubtree(subTree.getRoot(), writer);
+                    return new OctetStreamData(new ByteArrayInputStream(getC14nBytes(writer, isByteArrayOutputStream)));
                 }
             } else if (data instanceof NodeSetData) {
                 NodeSetData nsd = (NodeSetData)data;
@@ -167,24 +158,29 @@ public abstract class ApacheCanonicalize
                 nodeSet = ns;
                 LOG.debug("Canonicalizing {} nodes", nodeSet.size());
             } else {
-                return new OctetStreamData(new ByteArrayInputStream(
-                    canonicalizer.canonicalize(
-                        Utils.readBytesFromStream(
-                        ((OctetStreamData)data).getOctetStream()))));
+                canonicalizer.canonicalize(Utils.readBytesFromStream(((OctetStreamData)data).getOctetStream()), writer);
+                return new OctetStreamData(new ByteArrayInputStream(getC14nBytes(writer, isByteArrayOutputStream)));
             }
+
             if (inclusiveNamespaces != null) {
-                return new OctetStreamData(new ByteArrayInputStream(
-                    canonicalizer.canonicalizeXPathNodeSet
-                        (nodeSet, inclusiveNamespaces)));
+                canonicalizer.canonicalizeXPathNodeSet(nodeSet, inclusiveNamespaces, writer);
+                return new OctetStreamData(new ByteArrayInputStream(getC14nBytes(writer, isByteArrayOutputStream)));
             } else {
-                return new OctetStreamData(new ByteArrayInputStream(
-                    canonicalizer.canonicalizeXPathNodeSet(nodeSet)));
+                canonicalizer.canonicalizeXPathNodeSet(nodeSet, writer);
+                return new OctetStreamData(new ByteArrayInputStream(getC14nBytes(writer, isByteArrayOutputStream)));
             }
         } catch (Exception e) {
             throw new TransformException(e);
         }
     }
 
+    private byte[] getC14nBytes(OutputStream outputStream, boolean isByteArrayOutputStream) {    // NOPMD - preserving previous behavior here
+        if (isByteArrayOutputStream) {
+            return ((ByteArrayOutputStream)outputStream).toByteArray();
+        }
+        return null;
+    }
+
     public Data transform(Data data, XMLCryptoContext xc, OutputStream os)
         throws TransformException
     {

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java Wed Jan 22 13:06:34 2020
@@ -216,28 +216,28 @@ public final class Canonicalizer {
      * wrapped with a <CODE>&gt;a&lt;...&gt;/a&lt;</CODE>.
      *
      * @param inputBytes
-     * @return the result of the canonicalization.
+     * param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      * @throws java.io.IOException
      * @throws javax.xml.parsers.ParserConfigurationException
      * @throws org.xml.sax.SAXException
      */
-    public byte[] canonicalize(byte[] inputBytes)
+    public void canonicalize(byte[] inputBytes, OutputStream writer)
         throws javax.xml.parsers.ParserConfigurationException,
         java.io.IOException, org.xml.sax.SAXException, CanonicalizationException {
-        return canonicalizerSpi.engineCanonicalize(inputBytes);
+        canonicalizerSpi.engineCanonicalize(inputBytes, writer);
     }
 
     /**
      * Canonicalizes the subtree rooted by <CODE>node</CODE>.
      *
      * @param node The node to canonicalize
-     * @return the result of the c14n.
+     * param writer OutputStream to write the canonicalization result
      *
      * @throws CanonicalizationException
      */
-    public byte[] canonicalizeSubtree(Node node) throws CanonicalizationException {
-        return canonicalizerSpi.engineCanonicalizeSubTree(node);
+    public void canonicalizeSubtree(Node node, OutputStream writer) throws CanonicalizationException {
+        canonicalizerSpi.engineCanonicalizeSubTree(node, writer);
     }
 
     /**
@@ -245,12 +245,12 @@ public final class Canonicalizer {
      *
      * @param node
      * @param inclusiveNamespaces
-     * @return the result of the c14n.
+     * param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] canonicalizeSubtree(Node node, String inclusiveNamespaces)
+    public void canonicalizeSubtree(Node node, String inclusiveNamespaces, OutputStream writer)
         throws CanonicalizationException {
-        return canonicalizerSpi.engineCanonicalizeSubTree(node, inclusiveNamespaces);
+        canonicalizerSpi.engineCanonicalizeSubTree(node, inclusiveNamespaces, writer);
     }
 
     /**
@@ -258,24 +258,25 @@ public final class Canonicalizer {
      *
      * @param node
      * @param inclusiveNamespaces
-     * @return the result of the c14n.
+     * param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] canonicalizeSubtree(Node node, String inclusiveNamespaces, boolean propagateDefaultNamespace)
+    public void canonicalizeSubtree(Node node, String inclusiveNamespaces,
+                                    boolean propagateDefaultNamespace, OutputStream writer)
             throws CanonicalizationException {
-        return canonicalizerSpi.engineCanonicalizeSubTree(node, inclusiveNamespaces, propagateDefaultNamespace);
+        canonicalizerSpi.engineCanonicalizeSubTree(node, inclusiveNamespaces, propagateDefaultNamespace, writer);
     }
 
     /**
      * Canonicalizes an XPath node set.
      *
      * @param xpathNodeSet
-     * @return the result of the c14n.
+     * param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] canonicalizeXPathNodeSet(Set<Node> xpathNodeSet)
+    public void canonicalizeXPathNodeSet(Set<Node> xpathNodeSet, OutputStream writer)
         throws CanonicalizationException {
-        return canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet);
+        canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet, writer);
     }
 
     /**
@@ -283,22 +284,13 @@ public final class Canonicalizer {
      *
      * @param xpathNodeSet
      * @param inclusiveNamespaces
-     * @return the result of the c14n.
+     * param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] canonicalizeXPathNodeSet(
-        Set<Node> xpathNodeSet, String inclusiveNamespaces
+    public void canonicalizeXPathNodeSet(
+        Set<Node> xpathNodeSet, String inclusiveNamespaces, OutputStream writer
     ) throws CanonicalizationException {
-        return canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet, inclusiveNamespaces);
-    }
-
-    /**
-     * Sets the writer where the canonicalization ends.  ByteArrayOutputStream
-     * if none is set.
-     * @param os
-     */
-    public void setWriter(OutputStream os) {
-        canonicalizerSpi.setWriter(os);
+        canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet, inclusiveNamespaces, writer);
     }
 
     public boolean isSecureValidation() {

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java Wed Jan 22 13:06:34 2020
@@ -38,14 +38,14 @@ public abstract class CanonicalizerSpi {
      * Method canonicalize
      *
      * @param inputBytes
-     * @return the c14n bytes.
+     * @param writer OutputStream to write the canonicalization result
      *
      * @throws CanonicalizationException
      * @throws java.io.IOException
      * @throws javax.xml.parsers.ParserConfigurationException
      * @throws org.xml.sax.SAXException
      */
-    public byte[] engineCanonicalize(byte[] inputBytes)
+    public void engineCanonicalize(byte[] inputBytes, OutputStream writer)
         throws javax.xml.parsers.ParserConfigurationException, java.io.IOException,
         org.xml.sax.SAXException, CanonicalizationException {
 
@@ -53,7 +53,7 @@ public abstract class CanonicalizerSpi {
         try (java.io.InputStream bais = new ByteArrayInputStream(inputBytes)) {
             document = XMLUtils.read(bais, secureValidation);
         }
-        return this.engineCanonicalizeSubTree(document);
+        this.engineCanonicalizeSubTree(document, writer);
     }
 
     /**
@@ -72,10 +72,10 @@ public abstract class CanonicalizerSpi {
      * C14n a nodeset
      *
      * @param xpathNodeSet
-     * @return the c14n bytes
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public abstract byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet)
+    public abstract void engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, OutputStream writer)
         throws CanonicalizationException;
 
     /**
@@ -83,21 +83,21 @@ public abstract class CanonicalizerSpi {
      *
      * @param xpathNodeSet
      * @param inclusiveNamespaces
-     * @return the c14n bytes
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public abstract byte[] engineCanonicalizeXPathNodeSet(
-        Set<Node> xpathNodeSet, String inclusiveNamespaces
+    public abstract void engineCanonicalizeXPathNodeSet(
+        Set<Node> xpathNodeSet, String inclusiveNamespaces, OutputStream writer
     ) throws CanonicalizationException;
 
     /**
      * C14n a node tree.
      *
      * @param rootNode
-     * @return the c14n bytes
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public abstract byte[] engineCanonicalizeSubTree(Node rootNode)
+    public abstract void engineCanonicalizeSubTree(Node rootNode, OutputStream writer)
         throws CanonicalizationException;
 
     /**
@@ -105,10 +105,10 @@ public abstract class CanonicalizerSpi {
      *
      * @param rootNode
      * @param inclusiveNamespaces
-     * @return the c14n bytes
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public abstract byte[] engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces)
+    public abstract void engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces, OutputStream writer)
         throws CanonicalizationException;
 
     /**
@@ -117,20 +117,13 @@ public abstract class CanonicalizerSpi {
      * @param rootNode
      * @param inclusiveNamespaces
      * @param propagateDefaultNamespace If true the default namespace will be propagated to the c14n-ized root element
-     * @return the c14n bytes
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public abstract byte[] engineCanonicalizeSubTree(
-            Node rootNode, String inclusiveNamespaces, boolean propagateDefaultNamespace)
+    public abstract void engineCanonicalizeSubTree(
+            Node rootNode, String inclusiveNamespaces, boolean propagateDefaultNamespace, OutputStream writer)
             throws CanonicalizationException;
 
-    /**
-     * Sets the writer where the canonicalization ends. ByteArrayOutputStream if
-     * none is set.
-     * @param os
-     */
-    public abstract void setWriter(OutputStream os);
-
     public boolean isSecureValidation() {
         return secureValidation;
     }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java Wed Jan 22 13:06:34 2020
@@ -78,10 +78,10 @@ public abstract class Canonicalizer20010
      *
      * @param xpathNodeSet
      * @param inclusiveNamespaces
-     * @return none it always fails
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException always
      */
-    public byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, String inclusiveNamespaces)
+    public void engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, String inclusiveNamespaces, OutputStream writer)
         throws CanonicalizationException {
 
         /** $todo$ well, should we throw UnsupportedOperationException ? */
@@ -93,10 +93,10 @@ public abstract class Canonicalizer20010
      *
      * @param rootNode
      * @param inclusiveNamespaces
-     * @return none it always fails
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces)
+    public void engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces, OutputStream writer)
         throws CanonicalizationException {
 
         /** $todo$ well, should we throw UnsupportedOperationException ? */
@@ -108,11 +108,11 @@ public abstract class Canonicalizer20010
      *
      * @param rootNode
      * @param inclusiveNamespaces
-     * @return none it always fails
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(
-            Node rootNode, String inclusiveNamespaces, boolean propagateDefaultNamespace)
+    public void engineCanonicalizeSubTree(
+            Node rootNode, String inclusiveNamespaces, boolean propagateDefaultNamespace, OutputStream writer)
             throws CanonicalizationException {
 
         /** $todo$ well, should we throw UnsupportedOperationException ? */
@@ -131,11 +131,12 @@ public abstract class Canonicalizer20010
      * @param element
      * @param ns
      * @param cache
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException, DOMException, IOException
      */
     @Override
     protected void outputAttributesSubtree(Element element, NameSpaceSymbTable ns,
-                                           Map<String, byte[]> cache)
+                                           Map<String, byte[]> cache, OutputStream writer)
         throws CanonicalizationException, DOMException, IOException {
         if (!element.hasAttributes() && !firstCall) {
             return;
@@ -183,7 +184,6 @@ public abstract class Canonicalizer20010
             firstCall = false;
         }
 
-        OutputStream writer = getWriter();
         //we output all Attrs which are available
         for (Attr attr : result) {
             outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
@@ -200,11 +200,12 @@ public abstract class Canonicalizer20010
      * @param element
      * @param ns
      * @param cache
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException, DOMException, IOException
      */
     @Override
     protected void outputAttributes(Element element, NameSpaceSymbTable ns,
-                                    Map<String, byte[]> cache)
+                                    Map<String, byte[]> cache, OutputStream writer)
         throws CanonicalizationException, DOMException, IOException {
         // result will contain the attrs which have to be output
         xmlattrStack.push(ns.getLevel());
@@ -288,7 +289,6 @@ public abstract class Canonicalizer20010
             ns.getUnrenderedNodes(result);
         }
 
-        OutputStream writer = getWriter();
         //we output all Attrs which are available
         for (Attr attr : result) {
             outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java Wed Jan 22 13:06:34 2020
@@ -75,12 +75,12 @@ public abstract class Canonicalizer20010
      * Method engineCanonicalizeSubTree
      * {@inheritDoc}
      * @param rootNode
-     *
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(Node rootNode)
+    public void engineCanonicalizeSubTree(Node rootNode, OutputStream writer)
         throws CanonicalizationException {
-        return engineCanonicalizeSubTree(rootNode, "", null);
+        engineCanonicalizeSubTree(rootNode, "", null, writer);
     }
 
     /**
@@ -88,13 +88,13 @@ public abstract class Canonicalizer20010
      *  {@inheritDoc}
      * @param rootNode
      * @param inclusiveNamespaces
-     *
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(
-        Node rootNode, String inclusiveNamespaces
+    public void engineCanonicalizeSubTree(
+        Node rootNode, String inclusiveNamespaces, OutputStream writer
     ) throws CanonicalizationException {
-        return engineCanonicalizeSubTree(rootNode, inclusiveNamespaces, null);
+        engineCanonicalizeSubTree(rootNode, inclusiveNamespaces, null, writer);
     }
 
     /**
@@ -103,14 +103,14 @@ public abstract class Canonicalizer20010
      * @param rootNode
      * @param inclusiveNamespaces
      * @param propagateDefaultNamespace If true the default namespace will be propagated to the c14n-ized root element
-     *
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(
-            Node rootNode, String inclusiveNamespaces, boolean propagateDefaultNamespace
+    public void engineCanonicalizeSubTree(
+        Node rootNode, String inclusiveNamespaces, boolean propagateDefaultNamespace, OutputStream writer
     ) throws CanonicalizationException {
         this.propagateDefaultNamespace = propagateDefaultNamespace;
-        return engineCanonicalizeSubTree(rootNode, inclusiveNamespaces, null);
+        engineCanonicalizeSubTree(rootNode, inclusiveNamespaces, null, writer);
     }
 
     /**
@@ -118,28 +118,28 @@ public abstract class Canonicalizer20010
      * @param rootNode
      * @param inclusiveNamespaces
      * @param excl A element to exclude from the c14n process.
-     * @return the rootNode c14n.
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(
-        Node rootNode, String inclusiveNamespaces, Node excl
+    public void engineCanonicalizeSubTree(
+        Node rootNode, String inclusiveNamespaces, Node excl, OutputStream writer
     ) throws CanonicalizationException{
         inclusiveNSSet = InclusiveNamespaces.prefixStr2Set(inclusiveNamespaces);
-        return super.engineCanonicalizeSubTree(rootNode, excl);
+        super.engineCanonicalizeSubTree(rootNode, excl, writer);
     }
 
     /**
      *
      * @param rootNode
      * @param inclusiveNamespaces
-     * @return the rootNode c14n.
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalize(
-        XMLSignatureInput rootNode, String inclusiveNamespaces
+    public void engineCanonicalize(
+        XMLSignatureInput rootNode, String inclusiveNamespaces, OutputStream writer
     ) throws CanonicalizationException {
         inclusiveNSSet = InclusiveNamespaces.prefixStr2Set(inclusiveNamespaces);
-        return super.engineCanonicalize(rootNode);
+        super.engineCanonicalize(rootNode, writer);
     }
 
     /**
@@ -147,18 +147,19 @@ public abstract class Canonicalizer20010
      * {@inheritDoc}
      * @param xpathNodeSet
      * @param inclusiveNamespaces
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeXPathNodeSet(
-        Set<Node> xpathNodeSet, String inclusiveNamespaces
+    public void engineCanonicalizeXPathNodeSet(
+        Set<Node> xpathNodeSet, String inclusiveNamespaces, OutputStream writer
     ) throws CanonicalizationException {
         inclusiveNSSet = InclusiveNamespaces.prefixStr2Set(inclusiveNamespaces);
-        return super.engineCanonicalizeXPathNodeSet(xpathNodeSet);
+        super.engineCanonicalizeXPathNodeSet(xpathNodeSet, writer);
     }
 
     @Override
     protected void outputAttributesSubtree(Element element, NameSpaceSymbTable ns,
-                                           Map<String, byte[]> cache)
+                                           Map<String, byte[]> cache, OutputStream writer)
         throws CanonicalizationException, DOMException, IOException {
         // result will contain the attrs which have to be output
         SortedSet<Attr> result = new TreeSet<>(COMPARE);
@@ -223,7 +224,6 @@ public abstract class Canonicalizer20010
             }
         }
 
-        OutputStream writer = getWriter();
         //we output all Attrs which are available
         for (Attr attr : result) {
             outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
@@ -232,7 +232,7 @@ public abstract class Canonicalizer20010
 
     @Override
     protected void outputAttributes(Element element, NameSpaceSymbTable ns,
-                                    Map<String, byte[]> cache)
+                                    Map<String, byte[]> cache, OutputStream writer)
         throws CanonicalizationException, DOMException, IOException {
         // result will contain the attrs which have to be output
         SortedSet<Attr> result = new TreeSet<>(COMPARE);
@@ -325,7 +325,6 @@ public abstract class Canonicalizer20010
             }
         }
 
-        OutputStream writer = getWriter();
         //we output all Attrs which are available
         for (Attr attr : result) {
             outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java Wed Jan 22 13:06:34 2020
@@ -18,7 +18,6 @@
  */
 package org.apache.xml.security.c14n.implementations;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
@@ -38,7 +37,6 @@ import org.apache.xml.security.c14n.help
 import org.apache.xml.security.signature.NodeFilter;
 import org.apache.xml.security.signature.XMLSignatureInput;
 import org.apache.xml.security.utils.Constants;
-import org.apache.xml.security.utils.UnsyncByteArrayOutputStream;
 import org.apache.xml.security.utils.XMLUtils;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Comment;
@@ -87,11 +85,9 @@ public abstract class CanonicalizerBase
     private boolean includeComments;
     private Set<Node> xpathNodeSet;
 
-    private OutputStream writer = new ByteArrayOutputStream();
-
-   /**
-    * The null xmlns definition.
-    */
+    /**
+     * The null xmlns definition.
+     */
     private Attr nullNode;
 
     /**
@@ -107,53 +103,55 @@ public abstract class CanonicalizerBase
      * Method engineCanonicalizeSubTree
      * {@inheritDoc}
      * @param rootNode
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(Node rootNode)
+    public void engineCanonicalizeSubTree(Node rootNode, OutputStream writer)
         throws CanonicalizationException {
-        return engineCanonicalizeSubTree(rootNode, (Node)null);
+        engineCanonicalizeSubTree(rootNode, (Node)null, writer);
     }
 
     /**
      * Method engineCanonicalizeXPathNodeSet
      * {@inheritDoc}
      * @param xpathNodeSet
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet)
+    public void engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, OutputStream writer)
         throws CanonicalizationException {
         this.xpathNodeSet = xpathNodeSet;
-        return engineCanonicalizeXPathNodeSetInternal(XMLUtils.getOwnerDocument(this.xpathNodeSet));
+        engineCanonicalizeXPathNodeSetInternal(XMLUtils.getOwnerDocument(this.xpathNodeSet), writer);
     }
 
     /**
      * Canonicalizes a Subtree node.
      * @param input the root of the subtree to canicalize
-     * @return The canonicalize stream.
+     * @param writer OutputStream to write the canonicalization result
+     *
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalize(XMLSignatureInput input) throws CanonicalizationException {
+    public void engineCanonicalize(XMLSignatureInput input, OutputStream writer) throws CanonicalizationException {
         try {
             if (input.isExcludeComments()) {
                 includeComments = false;
             }
             if (input.isOctetStream()) {
-                return engineCanonicalize(input.getBytes());
+                engineCanonicalize(input.getBytes(), writer);
             }
             if (input.isElement()) {
-                return engineCanonicalizeSubTree(input.getSubNode(), input.getExcludeNode());
+                engineCanonicalizeSubTree(input.getSubNode(), input.getExcludeNode(), writer);
             } else if (input.isNodeSet()) {
                 nodeFilter = input.getNodeFilters();
 
                 circumventBugIfNeeded(input);
 
                 if (input.getSubNode() != null) {
-                    return engineCanonicalizeXPathNodeSetInternal(input.getSubNode());
+                    engineCanonicalizeXPathNodeSetInternal(input.getSubNode(), writer);
                 } else {
-                    return engineCanonicalizeXPathNodeSet(input.getNodeSet());
+                    engineCanonicalizeXPathNodeSet(input.getNodeSet(), writer);
                 }
             }
-            return null;
         } catch (ParserConfigurationException ex) {
             throw new CanonicalizationException(ex);
         } catch (IOException ex) {
@@ -164,27 +162,16 @@ public abstract class CanonicalizerBase
     }
 
     /**
-     * @param writer The writer to set.
-     */
-    public void setWriter(OutputStream writer) {
-        this.writer = writer;
-    }
-
-    protected OutputStream getWriter() {
-        return writer;
-    }
-
-    /**
      * Canonicalizes a Subtree node.
      *
      * @param rootNode
      *            the root of the subtree to canonicalize
      * @param excludeNode
      *            a node to be excluded from the canonicalize operation
-     * @return The canonicalize stream.
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    protected byte[] engineCanonicalizeSubTree(Node rootNode, Node excludeNode)
+    protected void engineCanonicalizeSubTree(Node rootNode, Node excludeNode, OutputStream writer)
         throws CanonicalizationException {
         try {
             NameSpaceSymbTable ns = new NameSpaceSymbTable();
@@ -194,21 +181,8 @@ public abstract class CanonicalizerBase
                 getParentNameSpaces((Element)rootNode, ns);
                 nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
             }
-            this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel, excludeNode);
-            this.writer.flush();
-            if (this.writer instanceof ByteArrayOutputStream) {
-                byte[] result = ((ByteArrayOutputStream)this.writer).toByteArray();
-                this.writer.close();
-                return result;
-            } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
-                byte[] result = ((UnsyncByteArrayOutputStream)this.writer).toByteArray();
-                this.writer.close();
-                return result;
-            } else {
-                this.writer.close();
-            }
-            return null;
-
+            this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel, excludeNode, writer);
+            writer.flush();
         } catch (UnsupportedEncodingException ex) {
             throw new CanonicalizationException(ex);
         } catch (IOException ex) {
@@ -225,19 +199,19 @@ public abstract class CanonicalizerBase
      * @param endnode
      * @param documentLevel
      * @param excludeNode
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      * @throws IOException
      */
     private void canonicalizeSubTree(
         Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel,
-        Node excludeNode
+        Node excludeNode, OutputStream writer
     ) throws CanonicalizationException, IOException {
         if (currentNode == null || isVisibleInt(currentNode) == -1) {
             return;
         }
         Node sibling = null;
         Node parentNode = null;
-        final OutputStream writer = this.writer;
         Map<String, byte[]> cache = new HashMap<>();
         do {
             switch (currentNode.getNodeType()) {
@@ -282,7 +256,7 @@ public abstract class CanonicalizerBase
                 String name = currentElement.getTagName();
                 UtfHelpper.writeByte(name, writer, cache);
 
-                outputAttributesSubtree(currentElement, ns, cache);
+                outputAttributesSubtree(currentElement, ns, cache, writer);
 
                 writer.write('>');
                 sibling = currentNode.getFirstChild();
@@ -329,25 +303,11 @@ public abstract class CanonicalizerBase
     }
 
 
-    private byte[] engineCanonicalizeXPathNodeSetInternal(Node doc)
+    private void engineCanonicalizeXPathNodeSetInternal(Node doc, OutputStream writer)
         throws CanonicalizationException {
         try {
-            this.canonicalizeXPathNodeSet(doc, doc);
-            this.writer.flush();
-            if (this.writer instanceof ByteArrayOutputStream) {
-                byte[] sol = ((ByteArrayOutputStream)this.writer).toByteArray();
-                this.writer.close();
-                return sol;
-            } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
-                byte[] result = ((UnsyncByteArrayOutputStream)this.writer).toByteArray();
-                this.writer.close();
-                return result;
-            } else {
-                this.writer.close();
-            }
-            return null;
-        } catch (UnsupportedEncodingException ex) {
-            throw new CanonicalizationException(ex);
+            this.canonicalizeXPathNodeSet(doc, doc, writer);
+            writer.flush();
         } catch (IOException ex) {
             throw new CanonicalizationException(ex);
         }
@@ -359,10 +319,11 @@ public abstract class CanonicalizerBase
      *
      * @param currentNode
      * @param endnode
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      * @throws IOException
      */
-    private void canonicalizeXPathNodeSet(Node currentNode, Node endnode)
+    private void canonicalizeXPathNodeSet(Node currentNode, Node endnode, OutputStream writer)
         throws CanonicalizationException, IOException {
         if (isVisibleInt(currentNode) == -1) {
             return;
@@ -442,7 +403,7 @@ public abstract class CanonicalizerBase
                     ns.push();
                 }
 
-                outputAttributes(currentElement, ns, cache);
+                outputAttributes(currentElement, ns, cache, writer);
 
                 if (currentNodeIsVisible) {
                     writer.write('>');
@@ -613,9 +574,11 @@ public abstract class CanonicalizerBase
      * @param element
      * @param ns
      * @param cache
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException, DOMException, IOException
      */
-    abstract void outputAttributes(Element element, NameSpaceSymbTable ns, Map<String, byte[]> cache)
+    abstract void outputAttributes(Element element, NameSpaceSymbTable ns,
+                                   Map<String, byte[]> cache, OutputStream writer)
         throws CanonicalizationException, DOMException, IOException;
 
     /**
@@ -624,9 +587,11 @@ public abstract class CanonicalizerBase
      * @param element
      * @param ns
      * @param cache
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException, DOMException, IOException
      */
-    abstract void outputAttributesSubtree(Element element, NameSpaceSymbTable ns, Map<String, byte[]> cache)
+    abstract void outputAttributesSubtree(Element element, NameSpaceSymbTable ns,
+                                          Map<String, byte[]> cache, OutputStream writer)
         throws CanonicalizationException, DOMException, IOException;
 
     abstract void circumventBugIfNeeded(XMLSignatureInput input)

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerPhysical.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerPhysical.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerPhysical.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerPhysical.java Wed Jan 22 13:06:34 2020
@@ -63,10 +63,10 @@ public class CanonicalizerPhysical exten
      *
      * @param xpathNodeSet
      * @param inclusiveNamespaces
-     * @return none it always fails
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException always
      */
-    public byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, String inclusiveNamespaces)
+    public void engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, String inclusiveNamespaces, OutputStream writer)
         throws CanonicalizationException {
 
         /** $todo$ well, should we throw UnsupportedOperationException ? */
@@ -78,10 +78,10 @@ public class CanonicalizerPhysical exten
      *
      * @param rootNode
      * @param inclusiveNamespaces
-     * @return none it always fails
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces)
+    public void engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces, OutputStream writer)
         throws CanonicalizationException {
 
         /** $todo$ well, should we throw UnsupportedOperationException ? */
@@ -93,11 +93,11 @@ public class CanonicalizerPhysical exten
      *
      * @param rootNode
      * @param inclusiveNamespaces
-     * @return none it always fails
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException
      */
-    public byte[] engineCanonicalizeSubTree(
-            Node rootNode, String inclusiveNamespaces, boolean propagateDefaultNamespace)
+    public void engineCanonicalizeSubTree(
+            Node rootNode, String inclusiveNamespaces, boolean propagateDefaultNamespace, OutputStream writer)
             throws CanonicalizationException {
 
         /** $todo$ well, should we throw UnsupportedOperationException ? */
@@ -116,11 +116,12 @@ public class CanonicalizerPhysical exten
      * @param element
      * @param ns
      * @param cache
+     * @param writer OutputStream to write the canonicalization result
      * @throws CanonicalizationException, DOMException, IOException
      */
     @Override
     protected void outputAttributesSubtree(Element element, NameSpaceSymbTable ns,
-                                           Map<String, byte[]> cache)
+                                           Map<String, byte[]> cache, OutputStream writer)
         throws CanonicalizationException, DOMException, IOException {
         if (element.hasAttributes()) {
             // result will contain all the attrs declared directly on that element
@@ -134,7 +135,6 @@ public class CanonicalizerPhysical exten
                 result.add(attribute);
             }
 
-            OutputStream writer = getWriter();
             //we output all Attrs which are available
             for (Attr attr : result) {
                 outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
@@ -144,7 +144,7 @@ public class CanonicalizerPhysical exten
 
     @Override
     protected void outputAttributes(Element element, NameSpaceSymbTable ns,
-                                    Map<String, byte[]> cache)
+                                    Map<String, byte[]> cache, OutputStream writer)
         throws CanonicalizationException, DOMException, IOException {
 
         /** $todo$ well, should we throw UnsupportedOperationException ? */

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java Wed Jan 22 13:06:34 2020
@@ -99,9 +99,8 @@ public abstract class AbstractSerializer
     public String serialize(NodeList content) throws Exception {
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
-            canon.setWriter(baos);
             for (int i = 0; i < content.getLength(); i++) {
-                canon.canonicalizeSubtree(content.item(i));
+                canon.canonicalizeSubtree(content.item(i), baos);
             }
             String ret = baos.toString(StandardCharsets.UTF_8.name());
             baos.reset();
@@ -121,9 +120,8 @@ public abstract class AbstractSerializer
     public byte[] serializeToByteArray(NodeList content) throws Exception {
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
-            canon.setWriter(baos);
             for (int i = 0; i < content.getLength(); i++) {
-                canon.canonicalizeSubtree(content.item(i));
+                canon.canonicalizeSubtree(content.item(i), baos);
             }
             return baos.toByteArray();
         }
@@ -138,8 +136,7 @@ public abstract class AbstractSerializer
     public String canonSerialize(Node node) throws Exception {
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
-            canon.setWriter(baos);
-            canon.canonicalizeSubtree(node);
+            canon.canonicalizeSubtree(node, baos);
             String ret = baos.toString(StandardCharsets.UTF_8.name());
             baos.reset();
             return ret;
@@ -155,8 +152,7 @@ public abstract class AbstractSerializer
     public byte[] canonSerializeToByteArray(Node node) throws Exception {
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
-            canon.setWriter(baos);
-            canon.canonicalizeSubtree(node);
+            canon.canonicalizeSubtree(node, baos);
             return baos.toByteArray();
         }
     }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/SignedInfo.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/SignedInfo.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/SignedInfo.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/SignedInfo.java Wed Jan 22 13:06:34 2020
@@ -19,6 +19,7 @@
 package org.apache.xml.security.signature;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -278,11 +279,14 @@ public class SignedInfo extends Manifest
             // the c14n is not a secure one and can rewrite the URIs or like
             // so reparse the SignedInfo to be sure
             try {
-                Canonicalizer c14nizer =
-                    Canonicalizer.getInstance(c14nMethodURI);
+                Canonicalizer c14nizer = Canonicalizer.getInstance(c14nMethodURI);
                 c14nizer.setSecureValidation(secureValidation);
 
-                byte[] c14nizedBytes = c14nizer.canonicalizeSubtree(element);
+                byte[] c14nizedBytes = null;
+                try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+                    c14nizer.canonicalizeSubtree(element, baos);
+                    c14nizedBytes = baos.toByteArray();
+                }
                 try (InputStream is = new ByteArrayInputStream(c14nizedBytes)) {
                     Document newdoc = XMLUtils.read(is, secureValidation);
                     Node imported = element.getOwnerDocument().importNode(
@@ -333,19 +337,23 @@ public class SignedInfo extends Manifest
      * @throws CanonicalizationException
      * @throws InvalidCanonicalizerException
      * @throws XMLSecurityException
+     * @throws IOException
      */
     public byte[] getCanonicalizedOctetStream()
-        throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException {
+        throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException, IOException {
         if (this.c14nizedBytes == null) {
             Canonicalizer c14nizer =
                 Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
             c14nizer.setSecureValidation(isSecureValidation());
 
             String inclusiveNamespaces = this.getInclusiveNamespaces();
-            if (inclusiveNamespaces == null) {
-                this.c14nizedBytes = c14nizer.canonicalizeSubtree(getElement());
-            } else {
-                this.c14nizedBytes = c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces);
+            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+                if (inclusiveNamespaces == null) {
+                    c14nizer.canonicalizeSubtree(getElement(), baos);
+                } else {
+                    c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces, baos);
+                }
+                this.c14nizedBytes = baos.toByteArray();
             }
         }
 
@@ -366,13 +374,12 @@ public class SignedInfo extends Manifest
             Canonicalizer c14nizer =
                 Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
             c14nizer.setSecureValidation(isSecureValidation());
-            c14nizer.setWriter(os);
             String inclusiveNamespaces = this.getInclusiveNamespaces();
 
             if (inclusiveNamespaces == null) {
-                c14nizer.canonicalizeSubtree(getElement());
+                c14nizer.canonicalizeSubtree(getElement(), os);
             } else {
-                c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces);
+                c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces, os);
             }
         } else {
             try {

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java Wed Jan 22 13:06:34 2020
@@ -19,6 +19,7 @@
 package org.apache.xml.security.signature;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -280,7 +281,10 @@ public class XMLSignatureInput {
             return inputBytes;
         }
         Canonicalizer20010315OmitComments c14nizer = new Canonicalizer20010315OmitComments();
-        bytes = c14nizer.engineCanonicalize(this);
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            c14nizer.engineCanonicalize(this, baos);
+            bytes = baos.toByteArray();
+        }
         return bytes;
     }
 
@@ -497,8 +501,7 @@ public class XMLSignatureInput {
             } else {
                 c14nizer = new Canonicalizer20010315OmitComments();
             }
-            c14nizer.setWriter(diOs);
-            c14nizer.engineCanonicalize(this);
+            c14nizer.engineCanonicalize(this, diOs);
         } else {
             byte[] buffer = new byte[4 * 1024];
             int bytesread = 0;

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N.java Wed Jan 22 13:06:34 2020
@@ -18,6 +18,8 @@
  */
 package org.apache.xml.security.transforms.implementations;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.xml.security.c14n.CanonicalizationException;
@@ -53,15 +55,23 @@ public class TransformC14N extends Trans
 
         Canonicalizer20010315OmitComments c14n = new Canonicalizer20010315OmitComments();
         c14n.setSecureValidation(secureValidation);
-        if (os != null) {
-            c14n.setWriter(os);
-        }
-        byte[] result = c14n.engineCanonicalize(input);
-        XMLSignatureInput output = new XMLSignatureInput(result);
-        output.setSecureValidation(secureValidation);
-        if (os != null) {
+
+        if (os == null) {
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, writer);
+                writer.flush();
+                XMLSignatureInput output = new XMLSignatureInput(writer.toByteArray());
+                output.setSecureValidation(secureValidation);
+                return output;
+            } catch (IOException ex) {
+                throw new CanonicalizationException("empty", new Object[] {ex.getMessage()});
+            }
+        } else {
+            c14n.engineCanonicalize(input, os);
+            XMLSignatureInput output = new XMLSignatureInput((byte[])null);
+            output.setSecureValidation(secureValidation);
             output.setOutputStream(os);
+            return output;
         }
-        return output;
     }
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11.java Wed Jan 22 13:06:34 2020
@@ -18,6 +18,8 @@
  */
 package org.apache.xml.security.transforms.implementations;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.xml.security.c14n.CanonicalizationException;
@@ -53,15 +55,23 @@ public class TransformC14N11 extends Tra
 
         Canonicalizer11_OmitComments c14n = new Canonicalizer11_OmitComments();
         c14n.setSecureValidation(secureValidation);
-        if (os != null) {
-            c14n.setWriter(os);
-        }
-        byte[] result = c14n.engineCanonicalize(input);
-        XMLSignatureInput output = new XMLSignatureInput(result);
-        output.setSecureValidation(secureValidation);
-        if (os != null) {
+
+        if (os == null) {
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, writer);
+                writer.flush();
+                XMLSignatureInput output = new XMLSignatureInput(writer.toByteArray());
+                output.setSecureValidation(secureValidation);
+                return output;
+            } catch (IOException ex) {
+                throw new CanonicalizationException("empty", new Object[] {ex.getMessage()});
+            }
+        } else {
+            c14n.engineCanonicalize(input, os);
+            XMLSignatureInput output = new XMLSignatureInput((byte[])null);
+            output.setSecureValidation(secureValidation);
             output.setOutputStream(os);
+            return output;
         }
-        return output;
     }
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11_WithComments.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11_WithComments.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11_WithComments.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14N11_WithComments.java Wed Jan 22 13:06:34 2020
@@ -18,6 +18,8 @@
  */
 package org.apache.xml.security.transforms.implementations;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.xml.security.c14n.CanonicalizationException;
@@ -53,16 +55,22 @@ public class TransformC14N11_WithComment
 
         Canonicalizer11_WithComments c14n = new Canonicalizer11_WithComments();
         c14n.setSecureValidation(secureValidation);
-        if (os != null) {
-            c14n.setWriter(os);
-        }
-
-        byte[] result = c14n.engineCanonicalize(input);
-        XMLSignatureInput output = new XMLSignatureInput(result);
-        output.setSecureValidation(secureValidation);
-        if (os != null) {
+        if (os == null) {
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, writer);
+                writer.flush();
+                XMLSignatureInput output = new XMLSignatureInput(writer.toByteArray());
+                output.setSecureValidation(secureValidation);
+                return output;
+            } catch (IOException ex) {
+                throw new CanonicalizationException("empty", new Object[] {ex.getMessage()});
+            }
+        } else {
+            c14n.engineCanonicalize(input, os);
+            XMLSignatureInput output = new XMLSignatureInput((byte[])null);
+            output.setSecureValidation(secureValidation);
             output.setOutputStream(os);
+            return output;
         }
-        return output;
     }
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NExclusive.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NExclusive.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NExclusive.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NExclusive.java Wed Jan 22 13:06:34 2020
@@ -18,6 +18,8 @@
  */
 package org.apache.xml.security.transforms.implementations;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.xml.security.c14n.CanonicalizationException;
@@ -76,17 +78,24 @@ public class TransformC14NExclusive exte
 
             Canonicalizer20010315Excl c14n = getCanonicalizer();
             c14n.setSecureValidation(secureValidation);
-            if (os != null) {
-                c14n.setWriter(os);
-            }
-            byte[] result = c14n.engineCanonicalize(input, inclusiveNamespaces);
 
-            XMLSignatureInput output = new XMLSignatureInput(result);
-            output.setSecureValidation(secureValidation);
-            if (os != null) {
+            if (os == null) {
+                try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                    c14n.engineCanonicalize(input, inclusiveNamespaces, writer);
+                    writer.flush();
+                    XMLSignatureInput output = new XMLSignatureInput(writer.toByteArray());
+                    output.setSecureValidation(secureValidation);
+                    return output;
+                } catch (IOException ex) {
+                    throw new CanonicalizationException("empty", new Object[] {ex.getMessage()});
+                }
+            } else {
+                c14n.engineCanonicalize(input, inclusiveNamespaces, os);
+                XMLSignatureInput output = new XMLSignatureInput((byte[])null);
+                output.setSecureValidation(secureValidation);
                 output.setOutputStream(os);
+                return output;
             }
-            return output;
         } catch (XMLSecurityException ex) {
             throw new CanonicalizationException(ex);
         }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NWithComments.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NWithComments.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NWithComments.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/implementations/TransformC14NWithComments.java Wed Jan 22 13:06:34 2020
@@ -18,6 +18,8 @@
  */
 package org.apache.xml.security.transforms.implementations;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.xml.security.c14n.CanonicalizationException;
@@ -53,16 +55,23 @@ public class TransformC14NWithComments e
 
         Canonicalizer20010315WithComments c14n = new Canonicalizer20010315WithComments();
         c14n.setSecureValidation(secureValidation);
-        if (os != null) {
-            c14n.setWriter(os);
-        }
 
-        byte[] result = c14n.engineCanonicalize(input);
-        XMLSignatureInput output = new XMLSignatureInput(result);
-        output.setSecureValidation(secureValidation);
-        if (os != null) {
+        if (os == null) {
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, writer);
+                writer.flush();
+                XMLSignatureInput output = new XMLSignatureInput(writer.toByteArray());
+                output.setSecureValidation(secureValidation);
+                return output;
+            } catch (IOException ex) {
+                throw new CanonicalizationException("empty", new Object[] {ex.getMessage()});
+            }
+        } else {
+            c14n.engineCanonicalize(input, os);
+            XMLSignatureInput output = new XMLSignatureInput((byte[])null);
+            output.setSecureValidation(secureValidation);
             output.setOutputStream(os);
+            return output;
         }
-        return output;
     }
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/utils/XMLUtils.java Wed Jan 22 13:06:34 2020
@@ -223,9 +223,8 @@ public final class XMLUtils {
                 os.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes(java.nio.charset.StandardCharsets.UTF_8));
             }
 
-            os.write(Canonicalizer.getInstance(
-                Canonicalizer.ALGO_ID_C14N_PHYSICAL).canonicalizeSubtree(contextNode)
-            );
+            Canonicalizer.getInstance(
+                Canonicalizer.ALGO_ID_C14N_PHYSICAL).canonicalizeSubtree(contextNode, os);
         } catch (IOException ex) {
             LOG.debug(ex.getMessage(), ex);
         }
@@ -251,12 +250,8 @@ public final class XMLUtils {
      */
     public static void outputDOMc14nWithComments(Node contextNode, OutputStream os) {
         try {
-            os.write(Canonicalizer.getInstance(
-                Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(contextNode)
-            );
-        } catch (IOException ex) {
-            LOG.debug(ex.getMessage(), ex);
-            // throw new RuntimeException(ex.getMessage());
+            Canonicalizer.getInstance(
+                Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(contextNode, os);
         } catch (InvalidCanonicalizerException ex) {
             LOG.debug(ex.getMessage(), ex);
             // throw new RuntimeException(ex.getMessage());

Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer11Test.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer11Test.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer11Test.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer11Test.java Wed Jan 22 13:06:34 2020
@@ -19,6 +19,7 @@
 package org.apache.xml.security.test.dom.c14n.implementations;
 
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.util.HashMap;
@@ -247,18 +248,22 @@ public class Canonicalizer11Test {
         Canonicalizer c14n = Canonicalizer.getInstance(c14nURI);
         byte[] c14nBytes = null;
 
-        if (xpath == null) {
-            c14nBytes = c14n.canonicalizeSubtree(doc);
-        } else {
-            XPathFactory xpf = XPathFactory.newInstance();
-            XPath xPath = xpf.newXPath();
-            DSNamespaceContext namespaceContext =
-                new DSNamespaceContext(namespaces);
-            xPath.setNamespaceContext(namespaceContext);
-
-            NodeList nl = (NodeList)xPath.evaluate(xpath, doc, XPathConstants.NODESET);
-
-            c14nBytes = c14n.canonicalizeXPathNodeSet(XMLUtils.convertNodelistToSet(nl));
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            if (xpath == null) {
+                c14n.canonicalizeSubtree(doc, baos);
+                c14nBytes = baos.toByteArray();
+            } else {
+                XPathFactory xpf = XPathFactory.newInstance();
+                XPath xPath = xpf.newXPath();
+                DSNamespaceContext namespaceContext =
+                    new DSNamespaceContext(namespaces);
+                xPath.setNamespaceContext(namespaceContext);
+
+                NodeList nl = (NodeList)xPath.evaluate(xpath, doc, XPathConstants.NODESET);
+
+                c14n.canonicalizeXPathNodeSet(XMLUtils.convertNodelistToSet(nl), baos);
+                c14nBytes = baos.toByteArray();
+            }
         }
 
         // org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);

Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer20010315ExclusiveTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer20010315ExclusiveTest.java?rev=1873032&r1=1873031&r2=1873032&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer20010315ExclusiveTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/c14n/implementations/Canonicalizer20010315ExclusiveTest.java Wed Jan 22 13:06:34 2020
@@ -20,6 +20,7 @@ package org.apache.xml.security.test.dom
 
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.nio.charset.StandardCharsets;
@@ -115,10 +116,12 @@ public class Canonicalizer20010315Exclus
         Canonicalizer20010315 c = new Canonicalizer20010315WithComments();
         byte[] reference = JavaUtils.getBytesFromFile(getAbsolutePath(
             "src/test/resources/org/apache/xml/security/c14n/inExcl/example2_2_1_c14nized.xml"));
-        byte[] result = c.engineCanonicalizeSubTree(root);
-        boolean equals = java.security.MessageDigest.isEqual(reference, result);
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c.engineCanonicalizeSubTree(root, writer);
+            boolean equals = java.security.MessageDigest.isEqual(reference, writer.toByteArray());
 
-        assertTrue(equals);
+            assertTrue(equals);
+        }
     }
 
     /**
@@ -134,10 +137,12 @@ public class Canonicalizer20010315Exclus
         Canonicalizer20010315 c = new Canonicalizer20010315WithComments();
         byte[] reference = JavaUtils.getBytesFromFile(getAbsolutePath(
             "src/test/resources/org/apache/xml/security/c14n/inExcl/example2_2_2_c14nized.xml"));
-        byte[] result = c.engineCanonicalizeSubTree(root);
-        boolean equals = java.security.MessageDigest.isEqual(reference, result);
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c.engineCanonicalizeSubTree(root, writer);
+            boolean equals = java.security.MessageDigest.isEqual(reference, writer.toByteArray());
 
-        assertTrue(equals);
+            assertTrue(equals);
+        }
     }
 
     /**
@@ -153,10 +158,12 @@ public class Canonicalizer20010315Exclus
         Canonicalizer20010315Excl c = new Canonicalizer20010315ExclWithComments();
         byte[] reference = JavaUtils.getBytesFromFile(getAbsolutePath(
             "src/test/resources/org/apache/xml/security/c14n/inExcl/example2_2_c14nized_exclusive.xml"));
-        byte[] result = c.engineCanonicalizeSubTree(root);
-        boolean equals = java.security.MessageDigest.isEqual(reference, result);
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c.engineCanonicalizeSubTree(root, writer);
+            boolean equals = java.security.MessageDigest.isEqual(reference, writer.toByteArray());
 
-        assertTrue(equals);
+            assertTrue(equals);
+        }
     }
 
     /**
@@ -172,10 +179,12 @@ public class Canonicalizer20010315Exclus
         Canonicalizer20010315Excl c = new Canonicalizer20010315ExclWithComments();
         byte[] reference = JavaUtils.getBytesFromFile(getAbsolutePath(
             "src/test/resources/org/apache/xml/security/c14n/inExcl/example2_2_c14nized_exclusive.xml") );
-        byte[] result = c.engineCanonicalizeSubTree(root);
-        boolean equals = java.security.MessageDigest.isEqual(reference, result);
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c.engineCanonicalizeSubTree(root, writer);
+            boolean equals = java.security.MessageDigest.isEqual(reference, writer.toByteArray());
 
-        assertTrue(equals);
+            assertTrue(equals);
+        }
     }
 
     /**
@@ -203,8 +212,10 @@ public class Canonicalizer20010315Exclus
             getAbsolutePath(
                 "src/test/resources/org/apache/xml/security/c14n/inExcl/example2_2_3_c14nized_exclusive.xml")
             );
-        byte[] result = c.engineCanonicalizeXPathNodeSet(XMLUtils.convertNodelistToSet(nodes));
-        assertEquals(new String(reference), new String(result));
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c.engineCanonicalizeXPathNodeSet(XMLUtils.convertNodelistToSet(nodes), writer);
+            assertEquals(new String(reference), new String(writer.toByteArray()));
+        }
     }
 
     /**
@@ -245,8 +256,10 @@ public class Canonicalizer20010315Exclus
         Set<Node> nodeSet = new HashSet<>();
         XMLUtils.getSet(doc.getDocumentElement().getFirstChild(), nodeSet, null, false);
         XMLSignatureInput input = new XMLSignatureInput(nodeSet);
-        byte[] bytes = c14n.engineCanonicalize(input, "env ns0 xsi wsu");
-        assertEquals(c14nXML, new String(bytes));
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c14n.engineCanonicalize(input, "env ns0 xsi wsu", writer);
+            assertEquals(c14nXML, new String(writer.toByteArray()));
+        }
     }
 
     /**
@@ -267,10 +280,12 @@ public class Canonicalizer20010315Exclus
         byte[] reference =
             JavaUtils.getBytesFromFile(getAbsolutePath(
                 "src/test/resources/org/apache/xml/security/c14n/inExcl/example2_4_c14nized.xml"));
-        byte[] result = c.engineCanonicalizeSubTree(root);
-        boolean equals = java.security.MessageDigest.isEqual(reference, result);
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c.engineCanonicalizeSubTree(root, writer);
+            boolean equals = java.security.MessageDigest.isEqual(reference, writer.toByteArray());
 
-        assertTrue(equals);
+            assertTrue(equals);
+        }
     }
 
     /**
@@ -295,10 +310,12 @@ public class Canonicalizer20010315Exclus
         byte[] reference =
             JavaUtils.getBytesFromFile(getAbsolutePath(
                 "src/test/resources/org/apache/xml/security/c14n/inExcl/example2_4_c14nized.xml"));
-        byte[] result = c.engineCanonicalizeSubTree(root);
-        boolean equals = java.security.MessageDigest.isEqual(reference, result);
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c.engineCanonicalizeSubTree(root, writer);
+            boolean equals = java.security.MessageDigest.isEqual(reference, writer.toByteArray());
 
-        assertTrue(equals);
+            assertTrue(equals);
+        }
     }
 
     /**
@@ -338,16 +355,20 @@ public class Canonicalizer20010315Exclus
             Canonicalizer20010315ExclOmitComments c14n =
                     new Canonicalizer20010315ExclOmitComments();
             XMLSignatureInput input = new XMLSignatureInput(doc.getDocumentElement().getFirstChild());
-            byte[] bytes = c14n.engineCanonicalize(input, "#default xsi");
-            assertEquals(c14nXML, new String(bytes));
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, "#default xsi", writer);
+                assertEquals(c14nXML, new String(writer.toByteArray()));
+            }
         }
         {
             //exactly the same outcome is expected if #default is not set:
             Canonicalizer20010315ExclOmitComments c14n =
                     new Canonicalizer20010315ExclOmitComments();
             XMLSignatureInput input = new XMLSignatureInput(doc.getDocumentElement().getFirstChild());
-            byte[] bytes = c14n.engineCanonicalize(input, "xsi");
-            assertEquals(c14nXML, new String(bytes));
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, "xsi", writer);
+                assertEquals(c14nXML, new String(writer.toByteArray()));
+            }
         }
     }
 
@@ -401,15 +422,19 @@ public class Canonicalizer20010315Exclus
             Canonicalizer20010315ExclOmitComments c14n =
                     new Canonicalizer20010315ExclOmitComments();
             XMLSignatureInput input = new XMLSignatureInput(doc.getDocumentElement().getFirstChild());
-            byte[] bytes = c14n.engineCanonicalize(input, "#default xsi");
-            assertEquals(c14nXML1, new String(bytes));
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, "#default xsi", writer);
+                assertEquals(c14nXML1, new String(writer.toByteArray()));
+            }
         }
         {
             Canonicalizer20010315ExclOmitComments c14n =
                     new Canonicalizer20010315ExclOmitComments();
             XMLSignatureInput input = new XMLSignatureInput(doc.getDocumentElement().getFirstChild());
-            byte[] bytes = c14n.engineCanonicalize(input, "xsi");
-            assertEquals(c14nXML2, new String(bytes));
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, "xsi", writer);
+                assertEquals(c14nXML2, new String(writer.toByteArray()));
+            }
         }
     }
 
@@ -451,16 +476,20 @@ public class Canonicalizer20010315Exclus
             Canonicalizer20010315ExclOmitComments c14n =
                     new Canonicalizer20010315ExclOmitComments();
             XMLSignatureInput input = new XMLSignatureInput(doc.getDocumentElement().getFirstChild());
-            byte[] bytes = c14n.engineCanonicalize(input, "#default xsi");
-            assertEquals(c14nXML, new String(bytes));
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, "#default xsi", writer);
+                assertEquals(c14nXML, new String(writer.toByteArray()));
+            }
         }
         {
             //exactly the same outcome is expected if #default is not set:
             Canonicalizer20010315ExclOmitComments c14n =
                     new Canonicalizer20010315ExclOmitComments();
             XMLSignatureInput input = new XMLSignatureInput(doc.getDocumentElement().getFirstChild());
-            byte[] bytes = c14n.engineCanonicalize(input, "xsi");
-            assertEquals(c14nXML, new String(bytes));
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, "xsi", writer);
+                assertEquals(c14nXML, new String(writer.toByteArray()));
+            }
         }
     }
 
@@ -501,16 +530,20 @@ public class Canonicalizer20010315Exclus
             Canonicalizer20010315ExclOmitComments c14n =
                     new Canonicalizer20010315ExclOmitComments();
             XMLSignatureInput input = new XMLSignatureInput(doc.getDocumentElement().getFirstChild());
-            byte[] bytes = c14n.engineCanonicalize(input, "#default xsi");
-            assertEquals(c14nXML, new String(bytes));
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, "#default xsi", writer);
+                assertEquals(c14nXML, new String(writer.toByteArray()));
+            }
         }
         {
             //exactly the same outcome is expected if #default is not set:
             Canonicalizer20010315ExclOmitComments c14n =
                     new Canonicalizer20010315ExclOmitComments();
             XMLSignatureInput input = new XMLSignatureInput(doc.getDocumentElement().getFirstChild());
-            byte[] bytes = c14n.engineCanonicalize(input, "xsi");
-            assertEquals(c14nXML, new String(bytes));
+            try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+                c14n.engineCanonicalize(input, "xsi", writer);
+                assertEquals(c14nXML, new String(writer.toByteArray()));
+            }
         }
     }
 
@@ -549,8 +582,10 @@ public class Canonicalizer20010315Exclus
         Document doc = XMLUtils.read(new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8)), false);
         Canonicalizer20010315ExclOmitComments c14n =
                 new Canonicalizer20010315ExclOmitComments();
-        byte[] bytes = c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild(), "#default", true);
-        assertEquals(c14nXML, new String(bytes));
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild(), "#default", true, writer);
+            assertEquals(c14nXML, new String(writer.toByteArray()));
+        }
     }
 
     @org.junit.jupiter.api.Test
@@ -584,8 +619,10 @@ public class Canonicalizer20010315Exclus
         Document doc = XMLUtils.read(new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8)), false);
         Canonicalizer20010315ExclOmitComments c14n =
                 new Canonicalizer20010315ExclOmitComments();
-        byte[] bytes = c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild(), "#default", true);
-        assertEquals(c14nXML, new String(bytes));
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild(), "#default", true, writer);
+            assertEquals(c14nXML, new String(writer.toByteArray()));
+        }
     }
 
     @org.junit.jupiter.api.Test
@@ -619,8 +656,10 @@ public class Canonicalizer20010315Exclus
         Document doc = XMLUtils.read(new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8)), false);
         Canonicalizer20010315ExclOmitComments c14n =
                 new Canonicalizer20010315ExclOmitComments();
-        byte[] bytes = c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild(), "#default", true);
-        assertEquals(c14nXML, new String(bytes));
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild(), "#default", true, writer);
+            assertEquals(c14nXML, new String(writer.toByteArray()));
+        }
     }
 
     @org.junit.jupiter.api.Test
@@ -654,8 +693,10 @@ public class Canonicalizer20010315Exclus
         Document doc = XMLUtils.read(new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8)), false);
         Canonicalizer20010315ExclOmitComments c14n =
                 new Canonicalizer20010315ExclOmitComments();
-        byte[] bytes = c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild(), "#default", true);
-        assertEquals(c14nXML, new String(bytes));
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild(), "#default", true, writer);
+            assertEquals(c14nXML, new String(writer.toByteArray()));
+        }
     }
 
     @org.junit.jupiter.api.Test
@@ -684,8 +725,10 @@ public class Canonicalizer20010315Exclus
         Document doc = XMLUtils.read(new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8)), false);
         Canonicalizer20010315ExclOmitComments c14n =
                 new Canonicalizer20010315ExclOmitComments();
-        byte[] bytes = c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild().getFirstChild(), "#default", true);
-        assertEquals(c14nXML, new String(bytes));
+        try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
+            c14n.engineCanonicalizeSubTree(doc.getDocumentElement().getFirstChild().getFirstChild(), "#default", true, writer);
+            assertEquals(c14nXML, new String(writer.toByteArray()));
+        }
     }
 
     private String getAbsolutePath(String path) {