Source code: org/mule/util/compression/GZipCompression.java
1 /*
2 * $Header: /cvsroot/mule/mule/src/java/org/mule/util/compression/GZipCompression.java,v 1.4 2003/10/20 21:44:37 rossmason Exp $
3 * $Revision: 1.4 $
4 * $Date: 2003/10/20 21:44:37 $
5 * ------------------------------------------------------------------------------------------------------
6 *
7 * Copyright (c) Cubis Limited. All rights reserved.
8 * http://www.cubis.co.uk
9 *
10 * The software in this package is published under the terms of the BSD
11 * style license a copy of which has been included with this distribution in
12 * the LICENSE.txt file.
13 *
14 */
15 package org.mule.util.compression;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.EOFException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.zip.GZIPInputStream;
23 import java.util.zip.GZIPOutputStream;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /**
29 * <p><code>GZipCompression</code> a CompressionStrategy implementation
30 * using the GZip library included in the JDK java.util.zip.
31 * This is the default CompressionStrategy used by the CompressionHelper
32 * discovery when no other implementation is discovered
33 *
34 * @author <a href="mailto:ross.mason@cubis.co.uk">Ross Mason</a>
35 * @version $Revision: 1.4 $
36 *
37 */
38 public class GZipCompression implements CompressionStrategy
39 {
40 /** The logger for this class */
41 private static final transient Log log = LogFactory.getLog(GZipCompression.class);
42
43 /**
44 * Determines if a byte array is compressed. The java.util.zip GZip implementaiton does not expose
45 * the GZip header so it is difficult to determine if a string is compressed.
46 * @param bytes an array of bytes
47 * @return true if the array is compressed or faluse otherwise
48 * @throws java.io.IOException if the byte array couldn't be read
49 */
50 public boolean isCompressed(byte[] bytes) throws IOException
51 {
52 //We only need the first 2 bytes
53 ByteArrayInputStream is = new ByteArrayInputStream(bytes, 0, 2);
54 int b = readByte(is);
55 b = (readByte(is) << 8) | b;
56
57 return (b == GZIPInputStream.GZIP_MAGIC);
58 }
59
60 /**
61 * @param in The InputStream to read the byte from
62 * @return The byte value
63 * @throws java.io.IOException if a byte could not be read
64 */
65 private int readByte(InputStream in) throws IOException
66 {
67 int b = in.read();
68 if (b == -1)
69 {
70 throw new EOFException();
71 }
72 return b;
73 }
74
75 /**
76 * Used for compressing a byte array into a new byte array using GZIP
77 * @param bytes An array of bytes to compress
78 * @return a compressed byte array
79 * @throws java.io.IOException if it fails to write to a GZIPOutputStream
80 * @see java.util.zip.GZIPOutputStream
81 */
82 public byte[] compressByteArray(byte[] bytes) throws IOException
83 {
84 log.debug("Compressing message of size: " + bytes.length);
85 ByteArrayOutputStream baos = new ByteArrayOutputStream();
86 GZIPOutputStream gos = new GZIPOutputStream(baos);
87 //gos.setMethod(ZipOutputStream.DEFLATED);
88 gos.write(bytes, 0, bytes.length);
89 gos.finish();
90 gos.close();
91 byte[] compressedByteArray = baos.toByteArray();
92 log.debug("Compressed message to size: " + compressedByteArray.length);
93 return compressedByteArray;
94 }
95
96 /**
97 * Used for uncompressing a byte array into a uncompressed byte array using GZIP
98 * @param bytes An array of bytes to uncompress
99 * @return an uncompressed byte array
100 * @throws java.io.IOException if it fails to read from a GZIPInputStream
101 * @see java.util.zip.GZIPInputStream
102 */
103 public byte[] uncompressByteArray(byte[] bytes) throws IOException
104 {
105 log.debug("Uncompressing message of size: " + bytes.length);
106 if (!isCompressed(bytes))
107 {
108 //throw a specific exception here to allow users of this method to deffientiate between
109 //general IOExceptions and an Invalid format
110 log.warn("data is not of type GZIP compressed. The data may not have been compressed in the first place");
111 throw new CompressionException("Not in GZIP format");
112 }
113
114 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
115 GZIPInputStream gis = new GZIPInputStream(bais);
116 ByteArrayOutputStream baos = new ByteArrayOutputStream();
117
118 byte[] buf = new byte[2048];
119 int len;
120
121 while ((len = gis.read(buf)) != -1)
122 {
123 baos.write(buf, 0, len);
124 }
125
126 byte[] uncompressedByteArray = baos.toByteArray();
127
128 log.debug("Uncompressing message to size: " + uncompressedByteArray.length);
129 return uncompressedByteArray;
130 }
131 }