1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.filter.support;
21
22 import java.io.IOException;
23
24 import org.apache.mina.common.ByteBuffer;
25
26 import com.jcraft.jzlib.JZlib;
27 import com.jcraft.jzlib.ZStream;
28
29
30
31
32
33
34
35
36
37 public class Zlib {
38 public static final int COMPRESSION_MAX = JZlib.Z_BEST_COMPRESSION;
39
40 public static final int COMPRESSION_MIN = JZlib.Z_BEST_SPEED;
41
42 public static final int COMPRESSION_NONE = JZlib.Z_NO_COMPRESSION;
43
44 public static final int COMPRESSION_DEFAULT = JZlib.Z_DEFAULT_COMPRESSION;
45
46 public static final int MODE_DEFLATER = 1;
47
48 public static final int MODE_INFLATER = 2;
49
50 private int compressionLevel;
51
52 private ZStream zStream = null;
53
54 private int mode = -1;
55
56
57
58
59
60
61 public Zlib(int compressionLevel, int mode) {
62 switch (compressionLevel) {
63 case COMPRESSION_MAX:
64 case COMPRESSION_MIN:
65 case COMPRESSION_NONE:
66 case COMPRESSION_DEFAULT:
67 this.compressionLevel = compressionLevel;
68 break;
69 default:
70 throw new IllegalArgumentException(
71 "invalid compression level specified");
72 }
73
74
75 zStream = new ZStream();
76
77 switch (mode) {
78 case MODE_DEFLATER:
79 zStream.deflateInit(this.compressionLevel);
80 break;
81 case MODE_INFLATER:
82 zStream.inflateInit();
83 break;
84 default:
85 throw new IllegalArgumentException("invalid mode specified");
86 }
87 this.mode = mode;
88 }
89
90
91
92
93
94
95
96
97
98 public ByteBuffer inflate(ByteBuffer inBuffer) throws IOException {
99 if (mode == MODE_DEFLATER) {
100 throw new IllegalStateException("not initialized as INFLATER");
101 }
102
103 byte[] inBytes = new byte[inBuffer.remaining()];
104 inBuffer.get(inBytes).flip();
105
106
107
108 byte[] outBytes = new byte[inBytes.length * 2];
109 ByteBuffer outBuffer = ByteBuffer.allocate(outBytes.length);
110 outBuffer.setAutoExpand(true);
111
112 zStream.next_in = inBytes;
113 zStream.next_in_index = 0;
114 zStream.avail_in = inBytes.length;
115 zStream.next_out = outBytes;
116 zStream.next_out_index = 0;
117 zStream.avail_out = outBytes.length;
118 int retval = 0;
119
120 do {
121 retval = zStream.inflate(JZlib.Z_SYNC_FLUSH);
122 switch (retval) {
123 case JZlib.Z_OK:
124
125 case JZlib.Z_BUF_ERROR:
126
127 outBuffer.put(outBytes, 0, zStream.next_out_index);
128 zStream.next_out_index = 0;
129 zStream.avail_out = outBytes.length;
130 break;
131 default:
132
133 outBuffer.release();
134 outBuffer = null;
135 if (zStream.msg == null)
136 throw new IOException("Unknown error. Error code : "
137 + retval);
138 else
139 throw new IOException("Unknown error. Error code : "
140 + retval + " and message : " + zStream.msg);
141 }
142 } while (zStream.avail_in > 0);
143
144 return outBuffer.flip();
145 }
146
147
148
149
150
151
152
153
154
155 public ByteBuffer deflate(ByteBuffer inBuffer) throws IOException {
156 if (mode == MODE_INFLATER) {
157 throw new IllegalStateException("not initialized as DEFLATER");
158 }
159
160 byte[] inBytes = new byte[inBuffer.remaining()];
161 inBuffer.get(inBytes).flip();
162
163
164
165
166 int outLen = (int) Math.round(inBytes.length * 1.001) + 1 + 12;
167 byte[] outBytes = new byte[outLen];
168
169 zStream.next_in = inBytes;
170 zStream.next_in_index = 0;
171 zStream.avail_in = inBytes.length;
172 zStream.next_out = outBytes;
173 zStream.next_out_index = 0;
174 zStream.avail_out = outBytes.length;
175
176 int retval = zStream.deflate(JZlib.Z_SYNC_FLUSH);
177 if (retval != JZlib.Z_OK) {
178 outBytes = null;
179 inBytes = null;
180 throw new IOException("Compression failed with return value : "
181 + retval);
182 }
183
184 ByteBuffer outBuf = ByteBuffer
185 .wrap(outBytes, 0, zStream.next_out_index);
186
187 return outBuf;
188 }
189
190
191
192
193 public void cleanUp() {
194 if (zStream != null)
195 zStream.free();
196 }
197 }