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