comparison artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java @ 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 c03d3a872cd2
children 8b58259d3dd3
comparison
equal deleted inserted replaced
233:16cd059945e5 234:23d642319a0b
5 * Read the file LGPL.txt coming with the software for details 5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist. 6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */ 7 */
8 8
9 package de.intevation.artifacts.common.utils; 9 package de.intevation.artifacts.common.utils;
10
11 import java.util.zip.GZIPInputStream;
12 import java.util.zip.GZIPOutputStream;
10 13
11 import java.io.ByteArrayInputStream; 14 import java.io.ByteArrayInputStream;
12 import java.io.FileInputStream; 15 import java.io.FileInputStream;
13 import java.io.BufferedInputStream; 16 import java.io.BufferedInputStream;
14 import java.io.ByteArrayOutputStream; 17 import java.io.ByteArrayOutputStream;
333 } 336 }
334 337
335 return false; 338 return false;
336 } 339 }
337 340
341 public static byte [] toByteArray(Document document) {
342 return toByteArray(document, false);
343 }
344
338 /** 345 /**
339 * Transforms an XML document into a byte array. 346 * Transforms an XML document into a byte array.
340 * @param document The document to be streamed out. 347 * @param document The document to be streamed out.
348 * @param compress The document should be compressed, too.
341 * @return the byte array or null if operation failed or 349 * @return the byte array or null if operation failed or
342 * document is null. 350 * document is null.
343 */ 351 */
344 public static byte [] toByteArray(Document document) { 352 public static byte [] toByteArray(Document document, boolean compress) {
345 if (document == null) { 353 if (document != null) {
346 return null; 354 try {
347 } 355 ByteArrayOutputStream baos = new ByteArrayOutputStream();
348 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 356 OutputStream out = compress
349 return toStream(document, baos) 357 ? new GZIPOutputStream(baos)
350 ? baos.toByteArray() 358 : baos;
351 : null; 359 boolean success = toStream(document, out);
360 out.flush();
361 out.close();
362 return success
363 ? baos.toByteArray()
364 : null;
365 }
366 catch (IOException ioe) {
367 logger.error(ioe);
368 }
369 }
370 return null;
352 } 371 }
353 372
354 public static Document fromByteArray(byte [] data) { 373 public static Document fromByteArray(byte [] data) {
355 if (data == null) { 374 return fromByteArray(data, false);
356 return null; 375 }
357 } 376
358 return parseDocument(new ByteArrayInputStream(data)); 377 public static Document fromByteArray(byte [] data, boolean decompress) {
378 if (data != null) {
379 InputStream in = new ByteArrayInputStream(data);
380 try {
381 if (decompress) {
382 in = new GZIPInputStream(in);
383 }
384 return parseDocument(in);
385 }
386 catch (IOException ioe) {
387 logger.error(ioe);
388 }
389 finally {
390 try {
391 in.close();
392 }
393 catch (IOException ioe) {
394 logger.error(ioe);
395 }
396 }
397 }
398 return null;
359 } 399 }
360 } 400 }
361 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 401 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org