Class JdkZlibEncoder
- java.lang.Object
-
- org.jboss.netty.handler.codec.oneone.OneToOneEncoder
-
- org.jboss.netty.handler.codec.oneone.OneToOneStrictEncoder
-
- org.jboss.netty.handler.codec.compression.JdkZlibEncoder
-
- All Implemented Interfaces:
ChannelDownstreamHandler
,ChannelHandler
,LifeCycleAwareChannelHandler
public class JdkZlibEncoder extends OneToOneStrictEncoder implements LifeCycleAwareChannelHandler
Compresses aChannelBuffer
using the deflate algorithm.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.zip.CRC32
crc
private ChannelHandlerContext
ctx
private java.util.zip.Deflater
deflater
private java.util.concurrent.atomic.AtomicBoolean
finished
private static byte[]
gzipHeader
private byte[]
out
private ZlibWrapper
wrapper
private boolean
writeHeader
-
Constructor Summary
Constructors Constructor Description JdkZlibEncoder()
Creates a new zlib encoder with the default compression level (6
) and the default wrapper (ZlibWrapper.ZLIB
).JdkZlibEncoder(byte[] dictionary)
Creates a new zlib encoder with the default compression level (6
) and the specified preset dictionary.JdkZlibEncoder(int compressionLevel)
Creates a new zlib encoder with the specifiedcompressionLevel
and the default wrapper (ZlibWrapper.ZLIB
).JdkZlibEncoder(int compressionLevel, byte[] dictionary)
Creates a new zlib encoder with the specifiedcompressionLevel
and the specified preset dictionary.JdkZlibEncoder(ZlibWrapper wrapper)
Creates a new zlib encoder with the default compression level (6
) and the specified wrapper.JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel)
Creates a new zlib encoder with the specifiedcompressionLevel
and the specified wrapper.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterAdd(ChannelHandlerContext ctx)
void
afterRemove(ChannelHandlerContext ctx)
void
beforeAdd(ChannelHandlerContext ctx)
void
beforeRemove(ChannelHandlerContext ctx)
ChannelFuture
close()
private void
deflate(ChannelBuffer out)
protected java.lang.Object
encode(ChannelHandlerContext ctx, Channel channel, java.lang.Object msg)
Transforms the specified message into another message and return the transformed message.private int
estimateCompressedSize(int originalSize)
private ChannelFuture
finishEncode(ChannelHandlerContext ctx, ChannelEvent evt)
void
handleDownstream(ChannelHandlerContext ctx, ChannelEvent evt)
Handles the specified downstream event.boolean
isClosed()
private boolean
isGzip()
-
Methods inherited from class org.jboss.netty.handler.codec.oneone.OneToOneStrictEncoder
doEncode
-
-
-
-
Field Detail
-
wrapper
private final ZlibWrapper wrapper
-
deflater
private final java.util.zip.Deflater deflater
-
finished
private final java.util.concurrent.atomic.AtomicBoolean finished
-
ctx
private volatile ChannelHandlerContext ctx
-
out
private byte[] out
-
crc
private final java.util.zip.CRC32 crc
-
gzipHeader
private static final byte[] gzipHeader
-
writeHeader
private boolean writeHeader
-
-
Constructor Detail
-
JdkZlibEncoder
public JdkZlibEncoder()
Creates a new zlib encoder with the default compression level (6
) and the default wrapper (ZlibWrapper.ZLIB
).- Throws:
CompressionException
- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(int compressionLevel)
Creates a new zlib encoder with the specifiedcompressionLevel
and the default wrapper (ZlibWrapper.ZLIB
).- Parameters:
compressionLevel
-1
yields the fastest compression and9
yields the best compression.0
means no compression. The default compression level is6
.- Throws:
CompressionException
- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(ZlibWrapper wrapper)
Creates a new zlib encoder with the default compression level (6
) and the specified wrapper.- Throws:
CompressionException
- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel)
Creates a new zlib encoder with the specifiedcompressionLevel
and the specified wrapper.- Parameters:
compressionLevel
-1
yields the fastest compression and9
yields the best compression.0
means no compression. The default compression level is6
.- Throws:
CompressionException
- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(byte[] dictionary)
Creates a new zlib encoder with the default compression level (6
) and the specified preset dictionary. The wrapper is alwaysZlibWrapper.ZLIB
because it is the only format that supports the preset dictionary.- Parameters:
dictionary
- the preset dictionary- Throws:
CompressionException
- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(int compressionLevel, byte[] dictionary)
Creates a new zlib encoder with the specifiedcompressionLevel
and the specified preset dictionary. The wrapper is alwaysZlibWrapper.ZLIB
because it is the only format that supports the preset dictionary.- Parameters:
compressionLevel
-1
yields the fastest compression and9
yields the best compression.0
means no compression. The default compression level is6
.dictionary
- the preset dictionary- Throws:
CompressionException
- if failed to initialize zlib
-
-
Method Detail
-
close
public ChannelFuture close()
-
isGzip
private boolean isGzip()
-
isClosed
public boolean isClosed()
-
encode
protected java.lang.Object encode(ChannelHandlerContext ctx, Channel channel, java.lang.Object msg) throws java.lang.Exception
Description copied from class:OneToOneEncoder
Transforms the specified message into another message and return the transformed message. Note that you can not returnnull
, unlike you can inOneToOneDecoder.decode(ChannelHandlerContext, Channel, Object)
; you must return something, at leastChannelBuffers.EMPTY_BUFFER
.- Specified by:
encode
in classOneToOneEncoder
- Throws:
java.lang.Exception
-
estimateCompressedSize
private int estimateCompressedSize(int originalSize)
-
handleDownstream
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent evt) throws java.lang.Exception
Description copied from interface:ChannelDownstreamHandler
Handles the specified downstream event.- Specified by:
handleDownstream
in interfaceChannelDownstreamHandler
- Overrides:
handleDownstream
in classOneToOneEncoder
- Parameters:
ctx
- the context object for this handlerevt
- the downstream event to process or intercept- Throws:
java.lang.Exception
-
finishEncode
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelEvent evt)
-
deflate
private void deflate(ChannelBuffer out)
-
beforeAdd
public void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
afterAdd
public void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
beforeRemove
public void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
afterRemove
public void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
-