1 10 11 package org.mule.util.compression; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.util.zip.GZIPInputStream ; 16 import java.util.zip.GZIPOutputStream ; 17 18 import org.apache.commons.io.IOUtils; 19 import org.apache.commons.io.output.ByteArrayOutputStream; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 29 public class GZipCompression implements CompressionStrategy 30 { 31 34 private static final Log logger = LogFactory.getLog(GZipCompression.class); 35 36 45 public boolean isCompressed(byte[] bytes) throws IOException 46 { 47 if ((bytes == null) || (bytes.length < 2)) 48 { 49 return false; 50 } 51 else 52 { 53 return ((bytes[0] == (byte)(GZIPInputStream.GZIP_MAGIC)) && (bytes[1] == (byte)(GZIPInputStream.GZIP_MAGIC >> 8))); 54 } 55 } 56 57 65 public byte[] compressByteArray(byte[] bytes) throws IOException 66 { 67 if (bytes == null || isCompressed(bytes)) 69 { 70 if (logger.isDebugEnabled()) 72 { 73 logger.debug("Data already compressed; doing nothing"); 74 } 75 return bytes; 76 } 77 78 if (logger.isDebugEnabled()) 79 { 80 logger.debug("Compressing message of size: " + bytes.length); 81 } 82 83 ByteArrayOutputStream baos = null; 84 GZIPOutputStream gzos = null; 85 86 try 87 { 88 baos = new ByteArrayOutputStream(32768); 89 gzos = new GZIPOutputStream (baos); 90 91 gzos.write(bytes, 0, bytes.length); 92 gzos.finish(); 93 gzos.close(); 94 95 byte[] compressedByteArray = baos.toByteArray(); 96 baos.close(); 97 98 if (logger.isDebugEnabled()) 99 { 100 logger.debug("Compressed message to size: " + compressedByteArray.length); 101 } 102 103 return compressedByteArray; 104 } 105 catch (IOException ioex) 106 { 107 throw ioex; 108 } 109 finally 110 { 111 IOUtils.closeQuietly(gzos); 112 IOUtils.closeQuietly(baos); 113 } 114 } 115 116 124 public byte[] uncompressByteArray(byte[] bytes) throws IOException 125 { 126 if (!isCompressed(bytes)) 128 { 129 136 137 if (logger.isDebugEnabled()) 139 { 140 logger.debug("Data already uncompressed; doing nothing"); 141 } 142 return bytes; 143 } 144 145 if (logger.isDebugEnabled()) 146 { 147 logger.debug("Uncompressing message of size: " + bytes.length); 148 } 149 150 ByteArrayInputStream bais = null; 151 GZIPInputStream gzis = null; 152 ByteArrayOutputStream baos = null; 153 154 try 155 { 156 bais = new ByteArrayInputStream (bytes); 157 gzis = new GZIPInputStream (bais); 158 baos = new ByteArrayOutputStream(32768); 159 160 IOUtils.copy(gzis, baos); 161 gzis.close(); 162 bais.close(); 163 164 byte[] uncompressedByteArray = baos.toByteArray(); 165 baos.close(); 166 167 if (logger.isDebugEnabled()) 168 { 169 logger.debug("Uncompressed message to size: " + uncompressedByteArray.length); 170 } 171 172 return uncompressedByteArray; 173 } 174 catch (IOException ioex) 175 { 176 throw ioex; 177 } 178 finally 179 { 180 IOUtils.closeQuietly(gzis); 181 IOUtils.closeQuietly(bais); 182 IOUtils.closeQuietly(baos); 183 } 184 } 185 186 } 187 | Popular Tags |