changeset 234:23d642319a0b

Added a boolean flag to XML byte serialisation to compress/decompress, too. artifacts/trunk@1641 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 03 Apr 2011 12:10:54 +0000
parents 16cd059945e5
children 3e29395ebac6
files ChangeLog artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java
diffstat 2 files changed, 56 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Apr 03 10:45:48 2011 +0000
+++ b/ChangeLog	Sun Apr 03 12:10:54 2011 +0000
@@ -1,3 +1,9 @@
+2011-04-03	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java:
+	  Added a boolean flag to XML byte serialisation to compress/decompress, too.
+	  Defaults to false to keep compatibilty.
+
 2011-04-03	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/**/*.java: Removed trailing whitespace.
--- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Sun Apr 03 10:45:48 2011 +0000
+++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Sun Apr 03 12:10:54 2011 +0000
@@ -8,6 +8,9 @@
 
 package de.intevation.artifacts.common.utils;
 
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.BufferedInputStream;
@@ -335,27 +338,64 @@
         return false;
     }
 
+    public static byte [] toByteArray(Document document) {
+        return toByteArray(document, false);
+    }
+
     /**
      * Transforms an XML document into a byte array.
      * @param document The document to be streamed out.
+     * @param compress The document should be compressed, too.
      * @return the byte array or null if operation failed or
      * document is null.
      */
-    public static byte [] toByteArray(Document document) {
-        if (document == null) {
-            return null;
+    public static byte [] toByteArray(Document document, boolean compress) {
+        if (document != null) {
+            try {
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                OutputStream out = compress
+                    ? new GZIPOutputStream(baos)
+                    : baos;
+                boolean success = toStream(document, out);
+                out.flush();
+                out.close();
+                return success
+                    ? baos.toByteArray()
+                    : null;
+            }
+            catch (IOException ioe) {
+                logger.error(ioe);
+            }
         }
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        return toStream(document, baos)
-            ? baos.toByteArray()
-            : null;
+        return null;
     }
 
     public static Document fromByteArray(byte [] data) {
-        if (data == null) {
-            return null;
+        return fromByteArray(data, false);
+    }
+
+    public static Document fromByteArray(byte [] data, boolean decompress) {
+        if (data != null) {
+            InputStream in = new ByteArrayInputStream(data);
+            try {
+                if (decompress) {
+                    in = new GZIPInputStream(in);
+                }
+                return parseDocument(in);
+            }
+            catch (IOException ioe) {
+                logger.error(ioe);
+            }
+            finally {
+                try {
+                    in.close();
+                }
+                catch (IOException ioe) {
+                    logger.error(ioe);
+                }
+            }
         }
-        return parseDocument(new ByteArrayInputStream(data));
+        return null;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org