View Javadoc

1   package org.apache.jcs.utils.zip;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.util.zip.DataFormatException;
26  import java.util.zip.Deflater;
27  import java.util.zip.GZIPInputStream;
28  import java.util.zip.Inflater;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /** Compress / Decompress. */
34  public final class CompressionUtil
35  {
36      /** The logger */
37      private final static Log log = LogFactory.getLog( CompressionUtil.class );
38  
39      /**
40       * no instances.
41       */
42      private CompressionUtil()
43      {
44          // NO OP
45      }
46  
47      /**
48       * Decompress the byte array passed using a default buffer length of 1024.
49       * <p>
50       * @param input compressed byte array webservice response
51       * @return uncompressed byte array
52       */
53      public static byte[] decompressByteArray( final byte[] input )
54      {
55          return decompressByteArray( input, 1024 );
56      }
57  
58      /**
59       * Decompress the byte array passed
60       * <p>
61       * @param input compressed byte array webservice response
62       * @param bufferLength buffer length
63       * @return uncompressed byte array
64       */
65      public static byte[] decompressByteArray( final byte[] input, final int bufferLength )
66      {
67          if ( null == input )
68          {
69              throw new IllegalArgumentException( "Input was null" );
70          }
71  
72          // Create the decompressor and give it the data to compress
73          final Inflater decompressor = new Inflater();
74  
75          decompressor.setInput( input );
76  
77          // Create an expandable byte array to hold the decompressed data
78          final ByteArrayOutputStream baos = new ByteArrayOutputStream( input.length );
79  
80          // Decompress the data
81          final byte[] buf = new byte[bufferLength];
82  
83          try
84          {
85              while ( !decompressor.finished() )
86              {
87                  int count = decompressor.inflate( buf );
88                  baos.write( buf, 0, count );
89              }
90          }
91          catch ( DataFormatException ex )
92          {
93              log.error( "Problem decompressing.", ex );
94          }
95  
96          try
97          {
98              baos.close();
99          }
100         catch ( IOException ex )
101         {
102             log.error( "Problem closing stream.", ex );
103         }
104 
105         return baos.toByteArray();
106     }
107 
108     /**
109      * Compress the byte array passed
110      * <p>
111      * @param input byte array
112      * @return compressed byte array
113      * @exception IOException thrown if we can't close the output stream
114      */
115     public static byte[] compressByteArray( byte[] input )
116         throws IOException
117     {
118         return compressByteArray( input, 1024 );
119     }
120 
121     /**
122      * Compress the byte array passed
123      * <p>
124      * @param input byte array
125      * @param bufferLength buffer length
126      * @return compressed byte array
127      * @exception IOException thrown if we can't close the output stream
128      */
129     public static byte[] compressByteArray( byte[] input, int bufferLength )
130         throws IOException
131     {
132         // Compressor with highest level of compression
133         Deflater compressor = new Deflater();
134         compressor.setLevel( Deflater.BEST_COMPRESSION );
135 
136         // Give the compressor the data to compress
137         compressor.setInput( input );
138         compressor.finish();
139 
140         // Create an expandable byte array to hold the compressed data.
141         // It is not necessary that the compressed data will be smaller than
142         // the uncompressed data.
143         ByteArrayOutputStream bos = new ByteArrayOutputStream( input.length );
144 
145         // Compress the data
146         byte[] buf = new byte[bufferLength];
147         while ( !compressor.finished() )
148         {
149             int count = compressor.deflate( buf );
150             bos.write( buf, 0, count );
151         }
152 
153         bos.close();
154 
155         // Get the compressed data
156         return bos.toByteArray();
157 
158     }
159 
160     /**
161      * decompress a gzip byte array, using a default buffer length of 1024
162      * <p>
163      * @param compressedByteArray gzip-compressed byte array
164      * @return decompressed byte array
165      * @throws IOException thrown if there was a failure to construct the GzipInputStream
166      */
167     public static byte[] decompressGzipByteArray( byte[] compressedByteArray )
168         throws IOException
169     {
170         return decompressGzipByteArray( compressedByteArray, 1024 );
171     }
172 
173     /**
174      * decompress a gzip byte array, using a default buffer length of 1024
175      * <p>
176      * @param compressedByteArray gzip-compressed byte array
177      * @param bufferlength size of the buffer in bytes
178      * @return decompressed byte array
179      * @throws IOException thrown if there was a failure to construct the GzipInputStream
180      */
181     public static byte[] decompressGzipByteArray( byte[] compressedByteArray, int bufferlength )
182         throws IOException
183     {
184         ByteArrayOutputStream uncompressedStream = new ByteArrayOutputStream();
185 
186         GZIPInputStream compressedStream = new GZIPInputStream( new ByteArrayInputStream( compressedByteArray ) );
187 
188         byte[] buffer = new byte[bufferlength];
189 
190         int index = -1;
191 
192         while ( ( index = compressedStream.read( buffer ) ) != -1 )
193         {
194             uncompressedStream.write( buffer, 0, index );
195         }
196 
197         return uncompressedStream.toByteArray();
198     }
199 }