# HG changeset patch # User Sascha L. Teichmann # Date 1301832654 0 # Node ID 23d642319a0bf4a0466379495a880d181b214df8 # Parent 16cd059945e5f0afc0e6b91b0e1dc0911bb07f76 Added a boolean flag to XML byte serialisation to compress/decompress, too. artifacts/trunk@1641 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 16cd059945e5 -r 23d642319a0b ChangeLog --- 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 + + * 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 * src/**/*.java: Removed trailing whitespace. diff -r 16cd059945e5 -r 23d642319a0b artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java --- 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 :