KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > CompressionHelper


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.util;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.util.zip.GZIPInputStream JavaDoc;
30 import java.util.zip.GZIPOutputStream JavaDoc;
31
32 import org.apache.log4j.Logger;
33
34 /**
35  * This helper class can compress and decompress a string. Useful in pagecache etc.
36  *
37  * @author mattias
38  */

39 public class CompressionHelper
40 {
41     private final static Logger logger = Logger.getLogger(CompressionHelper.class.getName());
42
43     public byte[] compress(String JavaDoc string)
44     {
45         byte[] bytes = null;
46         
47         try
48         {
49             ByteArrayOutputStream JavaDoc fos = new ByteArrayOutputStream JavaDoc();
50             GZIPOutputStream JavaDoc gz = new GZIPOutputStream JavaDoc(fos);
51             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(gz);
52             oos.writeObject(string);
53             //oos.writeObject("Mattias testar åäö ÅÄÖ");
54
oos.flush();
55             oos.close();
56             fos.close();
57             bytes = fos.toByteArray();
58
59             return bytes;
60         }
61         catch(Exception JavaDoc e)
62         {
63             logger.error("An error occurred when we tried to compress a string:" + e.getMessage(), e);
64         }
65         
66         try
67         {
68             bytes = string.getBytes("UTF-8");
69         }
70         catch (Exception JavaDoc e)
71         {
72             logger.error("An error occurred when we tried to just return the uncompressed bytes:" + e.getMessage(), e);
73         }
74
75         return bytes;
76     }
77     
78     public String JavaDoc decompress(byte[] bytes)
79     {
80         try
81         {
82             ByteArrayInputStream JavaDoc fis = new ByteArrayInputStream JavaDoc(bytes);
83             GZIPInputStream JavaDoc gs = new GZIPInputStream JavaDoc(fis);
84             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(gs);
85             String JavaDoc decompressed1 = (String JavaDoc)ois.readObject();
86             ois.close();
87             fis.close();
88             return decompressed1;
89         }
90         catch(Exception JavaDoc e)
91         {
92             logger.error("An error occurred when we tried to decompress a string:" + e.getMessage(), e);
93         }
94         
95         return "";
96     }
97
98 }
99
Popular Tags