Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/io/util/ByteArrayCompression.java


1   /** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  
19  package org.activemq.io.util;
20  import java.io.IOException;
21  import java.util.zip.Deflater;
22  import java.util.zip.GZIPInputStream;
23  
24  /**
25   * Compression stream
26   * 
27   * @version $Revision: 1.1.1.1 $
28   */
29  public class ByteArrayCompression{
30      
31      /**
32       * Data size above which compression will be used
33       */
34      public static final int DEFAULT_COMPRESSION_LIMIT = 32 * 1024;
35      
36      /**
37       * Default compression level - 0 being none, 9 being best
38       */
39      public static final int DEFAULT_COMPRESSION_LEVEL = Deflater.BEST_SPEED;
40      
41      /**
42       * Default Compression Strategy
43       */
44      public static final int DEFAULT_COMPRESSION_STRATEGY = Deflater.DEFAULT_STRATEGY;
45      
46      private int compressionLimit = DEFAULT_COMPRESSION_LIMIT;
47      private int compressionLevel = DEFAULT_COMPRESSION_LEVEL;
48      private int compressionStrategy = DEFAULT_COMPRESSION_STRATEGY;//default compression strategy
49      
50      /**
51       * @return Returns the compressionLevel.
52       */
53      public int getCompressionLevel() {
54          return compressionLevel;
55      }
56      /**
57       * @param compressionLevel The compressionLevel to set.
58       */
59      public void setCompressionLevel(int compressionLevel) {
60          this.compressionLevel = compressionLevel;
61      }
62      /**
63       * @return Returns the compressionLimit.
64       */
65      public int getCompressionLimit() {
66          return compressionLimit;
67      }
68      /**
69       * @param compressionLimit The compressionLimit to set.
70       */
71      public void setCompressionLimit(int compressionLimit) {
72          this.compressionLimit = compressionLimit;
73      }
74      /**
75       * @return Returns the compressionStrategy.
76       */
77      public int getCompressionStrategy() {
78          return compressionStrategy;
79      }
80      /**
81       * @param compressionStrategy The compressionStrategy to set.
82       */
83      public void setCompressionStrategy(int compressionStrategy) {
84          this.compressionStrategy = compressionStrategy;
85      }
86      /**
87       * test for compressed data
88       * @param ba
89       * @return true if the data in the ByteArray is compressed
90       */
91      public static boolean isCompressed(ByteArray ba){
92          boolean answer = false;
93          if (ba != null && ba.getLength() > 2){
94             answer = ((int)(ba.get(0) & 0xff) | (int)((ba.get(1) & 0xff)<< 8)) == GZIPInputStream.GZIP_MAGIC;
95          }
96          return answer; 
97      }
98      
99      /**
100      * Deflate the data in the ByteArray
101      * @param ba
102      * @return the passed in ba if data is not compressed else a new ByteArray
103      * @throws IOException
104      */
105     public ByteArray deflate(ByteArray ba) throws IOException{
106         ByteArray answer = ba;
107         if (ba != null && ba.getLength() > compressionLimit && !ByteArrayCompression.isCompressed(ba)){
108             WireByteArrayOutputStream bytesOut = new WireByteArrayOutputStream();
109             WireGZIPOutputStream gzipOut = new WireGZIPOutputStream(bytesOut);
110             gzipOut.getDeflater().setStrategy(compressionStrategy);
111             gzipOut.getDeflater().setLevel(compressionLevel);
112             gzipOut.write(ba.getBuf(),ba.getOffset(),ba.getLength());
113             gzipOut.close();
114             bytesOut.close();
115             answer = new ByteArray(bytesOut.getData(),0,bytesOut.size());
116         }
117         return answer;
118     }
119     
120     /**
121      * Inflate a ByteArray (if it contains compressed data)
122      * @param ba
123      * @return the inflated ByteArray
124      * @throws IOException
125      */
126     public ByteArray inflate(ByteArray ba) throws IOException {
127         ByteArray answer = ba;
128         if (ba != null && ByteArrayCompression.isCompressed(ba)){
129             WireByteArrayInputStream bytesIn = new WireByteArrayInputStream(ba);
130             GZIPInputStream gzipIn = new GZIPInputStream(bytesIn);
131             WireByteArrayOutputStream bytesOut = new WireByteArrayOutputStream();
132             byte[] buffer = new byte[1024];
133             int count = 0;
134             while ((count = gzipIn.read(buffer)) > 0 ){
135                 bytesOut.write(buffer,0,count);
136             }
137             bytesOut.close();
138             answer = new ByteArray(bytesOut.getData(),0,bytesOut.size());
139         }
140         return answer;
141     }
142         
143 }