changeset 135:a48882c9e096

Added a convinience method to transform an document into a byte array. artifacts/trunk@1360 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 02 Mar 2011 10:30:27 +0000
parents 19267b9960c3
children e2aad8d8ebd7
files ChangeLog artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java
diffstat 2 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Mar 02 10:21:19 2011 +0000
+++ b/ChangeLog	Wed Mar 02 10:30:27 2011 +0000
@@ -1,3 +1,9 @@
+2011-03-02	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java:
+	  Added a convinience method to transform an document
+	  into a byte array. Useful for storing documents as blob database columns.
+
 2011-03-02	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultUserFactory.java:
--- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Wed Mar 02 10:21:19 2011 +0000
+++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Wed Mar 02 10:30:27 2011 +0000
@@ -8,6 +8,7 @@
 
 package de.intevation.artifacts.common.utils;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -307,5 +308,21 @@
 
         return false;
     }
+
+    /**
+     * Transforms an XML document into a byte array.
+     * @param document The document to be streamed out.
+     * @return the byte array or null if operation failed or
+     * document is null.
+     */
+    public static byte [] toByteArray(Document document) {
+        if (document == null) {
+            return null;
+        }
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        return toStream(document, baos)
+            ? baos.toByteArray()
+            : null;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org