1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.codec.compression;
17
18 import com.jcraft.jzlib.Deflater;
19 import com.jcraft.jzlib.JZlib;
20 import io.netty.buffer.ByteBuf;
21 import io.netty.buffer.Unpooled;
22 import io.netty.channel.ChannelFuture;
23 import io.netty.channel.ChannelFutureListener;
24 import io.netty.channel.ChannelHandlerContext;
25 import io.netty.channel.ChannelPromise;
26 import io.netty.channel.ChannelPromiseNotifier;
27 import io.netty.util.concurrent.EventExecutor;
28 import io.netty.util.internal.EmptyArrays;
29 import io.netty.util.internal.OneTimeTask;
30
31 import java.util.concurrent.TimeUnit;
32
33 /**
34 * Compresses a [email protected] ByteBuf} using the deflate algorithm.
35 */
36 public class JZlibEncoder extends ZlibEncoder {
37
38 private final int wrapperOverhead;
39 private final Deflater z = new Deflater();
40 private volatile boolean finished;
41 private volatile ChannelHandlerContext ctx;
42
43 /**
44 * Creates a new zlib encoder with the default compression level ([email protected] 6}),
45 * default window bits ([email protected] 15}), default memory level ([email protected] 8}),
46 * and the default wrapper ([email protected] ZlibWrapper#ZLIB}).
47 *
48 * @throws CompressionException if failed to initialize zlib
49 */
50 public JZlibEncoder() {
51 this(6);
52 }
53
54 /**
55 * Creates a new zlib encoder with the specified [email protected] compressionLevel},
56 * default window bits ([email protected] 15}), default memory level ([email protected] 8}),
57 * and the default wrapper ([email protected] ZlibWrapper#ZLIB}).
58 *
59 * @param compressionLevel
60 * [email protected] 1} yields the fastest compression and [email protected] 9} yields the
61 * best compression. [email protected] 0} means no compression. The default
62 * compression level is [email protected] 6}.
63 *
64 * @throws CompressionException if failed to initialize zlib
65 */
66 public JZlibEncoder(int compressionLevel) {
67 this(ZlibWrapper.ZLIB, compressionLevel);
68 }
69
70 /**
71 * Creates a new zlib encoder with the default compression level ([email protected] 6}),
72 * default window bits ([email protected] 15}), default memory level ([email protected] 8}),
73 * and the specified wrapper.
74 *
75 * @throws CompressionException if failed to initialize zlib
76 */
77 public JZlibEncoder(ZlibWrapper wrapper) {
78 this(wrapper, 6);
79 }
80
81 /**
82 * Creates a new zlib encoder with the specified [email protected] compressionLevel},
83 * default window bits ([email protected] 15}), default memory level ([email protected] 8}),
84 * and the specified wrapper.
85 *
86 * @param compressionLevel
87 * [email protected] 1} yields the fastest compression and [email protected] 9} yields the
88 * best compression. [email protected] 0} means no compression. The default
89 * compression level is [email protected] 6}.
90 *
91 * @throws CompressionException if failed to initialize zlib
92 */
93 public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
94 this(wrapper, compressionLevel, 15, 8);
95 }
96
97 /**
98 * Creates a new zlib encoder with the specified [email protected] compressionLevel},
99 * the specified [email protected] windowBits}, the specified [email protected] memLevel}, and
100 * the specified wrapper.
101 *
102 * @param compressionLevel
103 * [email protected] 1} yields the fastest compression and [email protected] 9} yields the
104 * best compression. [email protected] 0} means no compression. The default
105 * compression level is [email protected] 6}.
106 * @param windowBits
107 * The base two logarithm of the size of the history buffer. The
108 * value should be in the range [email protected] 9} to [email protected] 15} inclusive.
109 * Larger values result in better compression at the expense of
110 * memory usage. The default value is [email protected] 15}.
111 * @param memLevel
112 * How much memory should be allocated for the internal compression
113 * state. [email protected] 1} uses minimum memory and [email protected] 9} uses maximum
114 * memory. Larger values result in better and faster compression
115 * at the expense of memory usage. The default value is [email protected] 8}
116 *
117 * @throws CompressionException if failed to initialize zlib
118 */
119 public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {
120
121 if (compressionLevel < 0 || compressionLevel > 9) {
122 throw new IllegalArgumentException(
123 "compressionLevel: " + compressionLevel +
124 " (expected: 0-9)");
125 }
126 if (windowBits < 9 || windowBits > 15) {
127 throw new IllegalArgumentException(
128 "windowBits: " + windowBits + " (expected: 9-15)");
129 }
130 if (memLevel < 1 || memLevel > 9) {
131 throw new IllegalArgumentException(
132 "memLevel: " + memLevel + " (expected: 1-9)");
133 }
134 if (wrapper == null) {
135 throw new NullPointerException("wrapper");
136 }
137 if (wrapper == ZlibWrapper.ZLIB_OR_NONE) {
138 throw new IllegalArgumentException(
139 "wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " +
140 "allowed for compression.");
141 }
142
143 int resultCode = z.init(
144 compressionLevel, windowBits, memLevel,
145 ZlibUtil.convertWrapperType(wrapper));
146 if (resultCode != JZlib.Z_OK) {
147 ZlibUtil.fail(z, "initialization failure", resultCode);
148 }
149
150 wrapperOverhead = ZlibUtil.wrapperOverhead(wrapper);
151 }
152
153 /**
154 * Creates a new zlib encoder with the default compression level ([email protected] 6}),
155 * default window bits ([email protected] 15}), default memory level ([email protected] 8}),
156 * and the specified preset dictionary. The wrapper is always
157 * [email protected] ZlibWrapper#ZLIB} because it is the only format that supports
158 * the preset dictionary.
159 *
160 * @param dictionary the preset dictionary
161 *
162 * @throws CompressionException if failed to initialize zlib
163 */
164 public JZlibEncoder(byte[] dictionary) {
165 this(6, dictionary);
166 }
167
168 /**
169 * Creates a new zlib encoder with the specified [email protected] compressionLevel},
170 * default window bits ([email protected] 15}), default memory level ([email protected] 8}),
171 * and the specified preset dictionary. The wrapper is always
172 * [email protected] ZlibWrapper#ZLIB} because it is the only format that supports
173 * the preset dictionary.
174 *
175 * @param compressionLevel
176 * [email protected] 1} yields the fastest compression and [email protected] 9} yields the
177 * best compression. [email protected] 0} means no compression. The default
178 * compression level is [email protected] 6}.
179 * @param dictionary the preset dictionary
180 *
181 * @throws CompressionException if failed to initialize zlib
182 */
183 public JZlibEncoder(int compressionLevel, byte[] dictionary) {
184 this(compressionLevel, 15, 8, dictionary);
185 }
186
187 /**
188 * Creates a new zlib encoder with the specified [email protected] compressionLevel},
189 * the specified [email protected] windowBits}, the specified [email protected] memLevel},
190 * and the specified preset dictionary. The wrapper is always
191 * [email protected] ZlibWrapper#ZLIB} because it is the only format that supports
192 * the preset dictionary.
193 *
194 * @param compressionLevel
195 * [email protected] 1} yields the fastest compression and [email protected] 9} yields the
196 * best compression. [email protected] 0} means no compression. The default
197 * compression level is [email protected] 6}.
198 * @param windowBits
199 * The base two logarithm of the size of the history buffer. The
200 * value should be in the range [email protected] 9} to [email protected] 15} inclusive.
201 * Larger values result in better compression at the expense of
202 * memory usage. The default value is [email protected] 15}.
203 * @param memLevel
204 * How much memory should be allocated for the internal compression
205 * state. [email protected] 1} uses minimum memory and [email protected] 9} uses maximum
206 * memory. Larger values result in better and faster compression
207 * at the expense of memory usage. The default value is [email protected] 8}
208 * @param dictionary the preset dictionary
209 *
210 * @throws CompressionException if failed to initialize zlib
211 */
212 public JZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
213 if (compressionLevel < 0 || compressionLevel > 9) {
214 throw new IllegalArgumentException("compressionLevel: " + compressionLevel + " (expected: 0-9)");
215 }
216 if (windowBits < 9 || windowBits > 15) {
217 throw new IllegalArgumentException(
218 "windowBits: " + windowBits + " (expected: 9-15)");
219 }
220 if (memLevel < 1 || memLevel > 9) {
221 throw new IllegalArgumentException(
222 "memLevel: " + memLevel + " (expected: 1-9)");
223 }
224 if (dictionary == null) {
225 throw new NullPointerException("dictionary");
226 }
227 int resultCode;
228 resultCode = z.deflateInit(
229 compressionLevel, windowBits, memLevel,
230 JZlib.W_ZLIB); // Default: ZLIB format
231 if (resultCode != JZlib.Z_OK) {
232 ZlibUtil.fail(z, "initialization failure", resultCode);
233 } else {
234 resultCode = z.deflateSetDictionary(dictionary, dictionary.length);
235 if (resultCode != JZlib.Z_OK) {
236 ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
237 }
238 }
239
240 wrapperOverhead = ZlibUtil.wrapperOverhead(ZlibWrapper.ZLIB);
241 }
242
243 @Override
244 public ChannelFuture close() {
245 return close(ctx().channel().newPromise());
246 }
247
248 @Override
249 public ChannelFuture close(final ChannelPromise promise) {
250 ChannelHandlerContext ctx = ctx();
251 EventExecutor executor = ctx.executor();
252 if (executor.inEventLoop()) {
253 return finishEncode(ctx, promise);
254 } else {
255 final ChannelPromise p = ctx.newPromise();
256 executor.execute(new OneTimeTask() {
257 @Override
258 public void run() {
259 ChannelFuture f = finishEncode(ctx(), p);
260 f.addListener(new ChannelPromiseNotifier(promise));
261 }
262 });
263 return p;
264 }
265 }
266
267 private ChannelHandlerContext ctx() {
268 ChannelHandlerContext ctx = this.ctx;
269 if (ctx == null) {
270 throw new IllegalStateException("not added to a pipeline");
271 }
272 return ctx;
273 }
274
275 @Override
276 public boolean isClosed() {
277 return finished;
278 }
279
280 @Override
281 protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
282 if (finished) {
283 out.writeBytes(in);
284 return;
285 }
286
287 int inputLength = in.readableBytes();
288 if (inputLength == 0) {
289 return;
290 }
291
292 try {
293 // Configure input.
294 boolean inHasArray = in.hasArray();
295 z.avail_in = inputLength;
296 if (inHasArray) {
297 z.next_in = in.array();
298 z.next_in_index = in.arrayOffset() + in.readerIndex();
299 } else {
300 byte[] array = new byte[inputLength];
301 in.getBytes(in.readerIndex(), array);
302 z.next_in = array;
303 z.next_in_index = 0;
304 }
305 int oldNextInIndex = z.next_in_index;
306
307 // Configure output.
308 int maxOutputLength = (int) Math.ceil(inputLength * 1.001) + 12 + wrapperOverhead;
309 out.ensureWritable(maxOutputLength);
310 z.avail_out = maxOutputLength;
311 z.next_out = out.array();
312 z.next_out_index = out.arrayOffset() + out.writerIndex();
313 int oldNextOutIndex = z.next_out_index;
314
315 // Note that Z_PARTIAL_FLUSH has been deprecated.
316 int resultCode;
317 try {
318 resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
319 } finally {
320 in.skipBytes(z.next_in_index - oldNextInIndex);
321 }
322
323 if (resultCode != JZlib.Z_OK) {
324 ZlibUtil.fail(z, "compression failure", resultCode);
325 }
326
327 int outputLength = z.next_out_index - oldNextOutIndex;
328 if (outputLength > 0) {
329 out.writerIndex(out.writerIndex() + outputLength);
330 }
331 } finally {
332 // Deference the external references explicitly to tell the VM that
333 // the allocated byte arrays are temporary so that the call stack
334 // can be utilized.
335 // I'm not sure if the modern VMs do this optimization though.
336 z.next_in = null;
337 z.next_out = null;
338 }
339 }
340
341 @Override
342 public void close(
343 final ChannelHandlerContext ctx,
344 final ChannelPromise promise) {
345 ChannelFuture f = finishEncode(ctx, ctx.newPromise());
346 f.addListener(new ChannelFutureListener() {
347 @Override
348 public void operationComplete(ChannelFuture f) throws Exception {
349 ctx.close(promise);
350 }
351 });
352
353 if (!f.isDone()) {
354 // Ensure the channel is closed even if the write operation completes in time.
355 ctx.executor().schedule(new OneTimeTask() {
356 @Override
357 public void run() {
358 ctx.close(promise);
359 }
360 }, 10, TimeUnit.SECONDS); // FIXME: Magic number
361 }
362 }
363
364 private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise) {
365 if (finished) {
366 promise.setSuccess();
367 return promise;
368 }
369 finished = true;
370
371 ByteBuf footer;
372 try {
373 // Configure input.
374 z.next_in = EmptyArrays.EMPTY_BYTES;
375 z.next_in_index = 0;
376 z.avail_in = 0;
377
378 // Configure output.
379 byte[] out = new byte[32]; // room for ADLER32 + ZLIB / CRC32 + GZIP header
380 z.next_out = out;
381 z.next_out_index = 0;
382 z.avail_out = out.length;
383
384 // Write the ADLER32 checksum (stream footer).
385 int resultCode = z.deflate(JZlib.Z_FINISH);
386 if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) {
387 promise.setFailure(ZlibUtil.deflaterException(z, "compression failure", resultCode));
388 return promise;
389 } else if (z.next_out_index != 0) {
390 footer = Unpooled.wrappedBuffer(out, 0, z.next_out_index);
391 } else {
392 footer = Unpooled.EMPTY_BUFFER;
393 }
394 } finally {
395 z.deflateEnd();
396
397 // Deference the external references explicitly to tell the VM that
398 // the allocated byte arrays are temporary so that the call stack
399 // can be utilized.
400 // I'm not sure if the modern VMs do this optimization though.
401 z.next_in = null;
402 z.next_out = null;
403 }
404 return ctx.writeAndFlush(footer, promise);
405 }
406
407 @Override
408 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
409 this.ctx = ctx;
410 }
411 }