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.common.IoBuffer;
25
26 import com.jcraft.jzlib.JZlib;
27 import com.jcraft.jzlib.ZStream;
28
29
30
31
32
33
34
35
36
37 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 public IoBuffer inflate(IoBuffer inBuffer) throws IOException {
98 if (mode == MODE_DEFLATER) {
99 throw new IllegalStateException("not initialized as INFLATER");
100 }
101
102 byte[] inBytes = new byte[inBuffer.limit()];
103 inBuffer.get(inBytes).flip();
104
105 byte[] outBytes = new byte[inBytes.length * 2];
106 IoBuffer outBuffer = IoBuffer.allocate(outBytes.length);
107 outBuffer.setAutoExpand(true);
108
109 zStream.next_in = inBytes;
110 zStream.next_in_index = 0;
111 zStream.avail_in = inBytes.length;
112 zStream.next_out = outBytes;
113 zStream.next_out_index = 0;
114 zStream.avail_out = outBytes.length;
115 int retval = 0;
116
117 do {
118 retval = zStream.inflate(JZlib.Z_SYNC_FLUSH);
119 switch (retval) {
120 case JZlib.Z_OK:
121
122 case JZlib.Z_BUF_ERROR:
123
124 outBuffer.put(outBytes, 0, zStream.next_out_index);
125 zStream.next_out_index = 0;
126 zStream.avail_out = outBytes.length;
127 break;
128 default:
129
130 outBuffer = null;
131 if (zStream.msg == null) {
132 throw new IOException("Unknown error. Error code : "
133 + retval);
134 } else {
135 throw new IOException("Unknown error. Error code : "
136 + retval + " and message : " + zStream.msg);
137 }
138 }
139 } while (zStream.avail_in > 0);
140
141 return outBuffer.flip();
142 }
143
144
145
146
147
148
149
150 public IoBuffer deflate(IoBuffer inBuffer) throws IOException {
151 if (mode == MODE_INFLATER) {
152 throw new IllegalStateException("not initialized as DEFLATER");
153 }
154
155 byte[] inBytes = new byte[inBuffer.limit()];
156 inBuffer.get(inBytes).flip();
157
158
159
160
161 int outLen = (int) Math.round(inBytes.length * 1.001) + 1 + 12;
162 byte[] outBytes = new byte[outLen];
163
164 zStream.next_in = inBytes;
165 zStream.next_in_index = 0;
166 zStream.avail_in = inBytes.length;
167 zStream.next_out = outBytes;
168 zStream.next_out_index = 0;
169 zStream.avail_out = outBytes.length;
170
171 int retval = zStream.deflate(JZlib.Z_SYNC_FLUSH);
172 if (retval != JZlib.Z_OK) {
173 outBytes = null;
174 inBytes = null;
175 throw new IOException("Compression failed with return value : "
176 + retval);
177 }
178
179 IoBuffer outBuf = IoBuffer
180 .wrap(outBytes, 0, zStream.next_out_index);
181
182 return outBuf;
183 }
184
185
186
187
188 public void cleanUp() {
189 if (zStream != null) {
190 zStream.free();
191 }
192 }
193 }