From 96cb5759869be3e02f011e3d81294576c6e265ad Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 21 Oct 2009 10:26:41 +0000 Subject: [PATCH] Complete support for DEFLATE based wrappers - zlib, gzip, no wrapper --- .../codec/compression/ZlibDecoder.java | 23 ++++++-- .../codec/compression/ZlibEncoder.java | 52 +++++++++++++++---- .../handler/codec/compression/ZlibUtil.java | 19 +++++++ 3 files changed, 79 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/jboss/netty/handler/codec/compression/ZlibDecoder.java b/src/main/java/org/jboss/netty/handler/codec/compression/ZlibDecoder.java index cd133107ca..22e2b4030d 100644 --- a/src/main/java/org/jboss/netty/handler/codec/compression/ZlibDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/compression/ZlibDecoder.java @@ -38,16 +38,27 @@ public class ZlibDecoder extends OneToOneDecoder { private final ZStream z = new ZStream(); private volatile boolean finished; - // TODO Auto-detect wrappers (zlib, gzip, nowrapper as a fallback) - /** - * Creates a new instance. + * Creates a new instance with the default wrapper ({@link ZlibWrapper#ZLIB}). * * @throws CompressionException if failed to initialize zlib */ public ZlibDecoder() { + this(ZlibWrapper.ZLIB); + } + + /** + * Creates a new instance with the specified wrapper. + * + * @throws CompressionException if failed to initialize zlib + */ + public ZlibDecoder(ZlibWrapper wrapper) { + if (wrapper == null) { + throw new NullPointerException("wrapper"); + } + synchronized (z) { - int resultCode = z.inflateInit(JZlib.W_ZLIB); + int resultCode = z.inflateInit(ZlibUtil.convertWrapperType(wrapper)); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); } @@ -55,7 +66,9 @@ public class ZlibDecoder extends OneToOneDecoder { } /** - * Creates a new instance with the specified preset dictionary. + * Creates a new instance with the specified preset dictionary. The wrapper + * is always {@link ZlibWrapper#ZLIB} because it is the only format that + * supports the preset dictionary. * * @throws CompressionException if failed to initialize zlib */ diff --git a/src/main/java/org/jboss/netty/handler/codec/compression/ZlibEncoder.java b/src/main/java/org/jboss/netty/handler/codec/compression/ZlibEncoder.java index 9f7c452135..bbbd62af27 100644 --- a/src/main/java/org/jboss/netty/handler/codec/compression/ZlibEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/compression/ZlibEncoder.java @@ -47,12 +47,9 @@ public class ZlibEncoder extends OneToOneEncoder { private final ZStream z = new ZStream(); private final AtomicBoolean finished = new AtomicBoolean(); - // TODO 'do not compress' once option - // TODO support three wrappers - zlib (default), gzip (unsupported by jzlib, but easy to implement), nowrap - // TODO Disallow preset dictionary for gzip - /** - * Creates a new zlib encoder with the default compression level ({@code 6}). + * Creates a new zlib encoder with the default compression level ({@code 6}) + * and the default wrapper ({@link ZlibWrapper#ZLIB}). * * @throws CompressionException if failed to initialize zlib */ @@ -61,7 +58,8 @@ public class ZlibEncoder extends OneToOneEncoder { } /** - * Creates a new zlib encoder with the specified {@code compressionLevel}. + * Creates a new zlib encoder with the specified {@code compressionLevel} + * and the default wrapper ({@link ZlibWrapper#ZLIB}). * * @param compressionLevel * {@code 1} yields the fastest compression and {@code 9} yields the @@ -71,12 +69,42 @@ public class ZlibEncoder extends OneToOneEncoder { * @throws CompressionException if failed to initialize zlib */ public ZlibEncoder(int compressionLevel) { + this(compressionLevel, ZlibWrapper.ZLIB); + } + + /** + * Creates a new zlib encoder with the default compression level ({@code 6}) + * and the specified wrapper. + * + * @throws CompressionException if failed to initialize zlib + */ + public ZlibEncoder(ZlibWrapper wrapper) { + this(6, wrapper); + } + + /** + * Creates a new zlib encoder with the specified {@code compressionLevel} + * and the specified wrapper. + * + * @param compressionLevel + * {@code 1} yields the fastest compression and {@code 9} yields the + * best compression. {@code 0} means no compression. The default + * compression level is {@code 6}. + * + * @throws CompressionException if failed to initialize zlib + */ + public ZlibEncoder(int compressionLevel, ZlibWrapper wrapper) { if (compressionLevel < 0 || compressionLevel > 9) { - throw new IllegalArgumentException("compressionLevel: " + compressionLevel + " (expected: 0-9)"); + throw new IllegalArgumentException( + "compressionLevel: " + compressionLevel + + " (expected: 0-9)"); + } + if (wrapper == null) { + throw new NullPointerException("wrapper"); } synchronized (z) { - int resultCode = z.deflateInit(compressionLevel, JZlib.W_ZLIB); // Default: ZLIB format + int resultCode = z.deflateInit(compressionLevel, ZlibUtil.convertWrapperType(wrapper)); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); } @@ -85,7 +113,9 @@ public class ZlibEncoder extends OneToOneEncoder { /** * Creates a new zlib encoder with the default compression level ({@code 6}) - * and the specified preset dictionary. + * and the specified preset dictionary. The wrapper is always + * {@link ZlibWrapper#ZLIB} because it is the only format that supports + * the preset dictionary. * * @param dictionary the preset dictionary * @@ -97,7 +127,9 @@ public class ZlibEncoder extends OneToOneEncoder { /** * Creates a new zlib encoder with the specified {@code compressionLevel} - * and the specified preset dictionary. + * and the specified preset dictionary. The wrapper is always + * {@link ZlibWrapper#ZLIB} because it is the only format that supports + * the preset dictionary. * * @param compressionLevel * {@code 1} yields the fastest compression and {@code 9} yields the diff --git a/src/main/java/org/jboss/netty/handler/codec/compression/ZlibUtil.java b/src/main/java/org/jboss/netty/handler/codec/compression/ZlibUtil.java index ec43d1f5fd..3973f9f2f1 100644 --- a/src/main/java/org/jboss/netty/handler/codec/compression/ZlibUtil.java +++ b/src/main/java/org/jboss/netty/handler/codec/compression/ZlibUtil.java @@ -15,6 +15,7 @@ */ package org.jboss.netty.handler.codec.compression; +import org.jboss.netty.util.internal.jzlib.JZlib; import org.jboss.netty.util.internal.jzlib.ZStream; /** @@ -35,6 +36,24 @@ final class ZlibUtil { (z.msg != null? ": " + z.msg : "")); } + static Enum convertWrapperType(ZlibWrapper wrapper) { + Enum convertedWrapperType; + switch (wrapper) { + case NONE: + convertedWrapperType = JZlib.W_NONE; + break; + case ZLIB: + convertedWrapperType = JZlib.W_ZLIB; + break; + case GZIP: + convertedWrapperType = JZlib.W_GZIP; + break; + default: + throw new Error(); + } + return convertedWrapperType; + } + private ZlibUtil() { super(); }