org.apache.mina.filter.compression
Class CompressionFilter

java.lang.Object
  extended by org.apache.mina.common.IoFilterAdapter
      extended by org.apache.mina.filter.util.WriteRequestFilter
          extended by org.apache.mina.filter.compression.CompressionFilter
All Implemented Interfaces:
IoFilter

public class CompressionFilter
extends WriteRequestFilter

An IoFilter which compresses all data using JZlib. Support for the LZW (DLCZ) algorithm is also planned.

This filter only supports compression using the PARTIAL FLUSH method, since that is the only method useful when doing stream level compression.

This filter supports compression/decompression of the input and output channels selectively. It can also be enabled/disabled on the fly.

This filter does not discard the zlib objects, keeping them around for the entire life of the filter. This is because the zlib dictionary needs to be built up over time, which is used during compression and decompression. Over time, as repetitive data is sent over the wire, the compression efficiency steadily increases.

Note that the zlib header is written only once. It is not necessary that the data received after processing by this filter may not be complete due to packet fragmentation.

It goes without saying that the other end of this stream should also have a compatible compressor/decompressor using the same algorithm.

Version:
$Rev: 581234 $, $Date: 2007-10-02 15:39:48 +0200 (Tue, 02 Oct 2007) $
Author:
The Apache MINA Project (dev@mina.apache.org)

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.mina.common.IoFilter
IoFilter.NextFilter
 
Field Summary
static int COMPRESSION_DEFAULT
          The default compression level used.
static int COMPRESSION_MAX
          Max compression level.
static int COMPRESSION_MIN
          Provides the best speed at the price of a low compression ratio.
static int COMPRESSION_NONE
          No compression done on the data.
static AttributeKey DISABLE_COMPRESSION_ONCE
          A flag that allows you to disable compression once.
 
Constructor Summary
CompressionFilter()
          Creates a new instance which compresses outboud data and decompresses inbound data with default compression level.
CompressionFilter(boolean compressInbound, boolean compressOutbound, int compressionLevel)
          Creates a new instance.
CompressionFilter(int compressionLevel)
          Creates a new instance which compresses outboud data and decompresses inbound data with the specified compressionLevel.
 
Method Summary
protected  Object doFilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
           
 boolean isCompressInbound()
          Returns true if incoming data is being compressed.
 boolean isCompressOutbound()
          Returns true if the filter is compressing data being written.
 void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message)
          Filters IoHandler.messageReceived(IoSession,Object) event.
 void onPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter)
          Invoked after this filter is removed from the specified parent.
 void onPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter)
          Invoked before this filter is added to the specified parent.
 void setCompressInbound(boolean compressInbound)
          Sets if incoming data has to be compressed.
 void setCompressOutbound(boolean compressOutbound)
          Set if outgoing data has to be compressed.
 
Methods inherited from class org.apache.mina.filter.util.WriteRequestFilter
exceptionCaught, filterWrite, messageSent
 
Methods inherited from class org.apache.mina.common.IoFilterAdapter
destroy, filterClose, filterSetTrafficMask, init, onPostAdd, onPreRemove, sessionClosed, sessionCreated, sessionIdle, sessionOpened
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMPRESSION_MAX

public static final int COMPRESSION_MAX
Max compression level. Will give the highest compression ratio, but will also take more cpu time and is the slowest.

See Also:
Constant Field Values

COMPRESSION_MIN

public static final int COMPRESSION_MIN
Provides the best speed at the price of a low compression ratio.

See Also:
Constant Field Values

COMPRESSION_NONE

public static final int COMPRESSION_NONE
No compression done on the data.

See Also:
Constant Field Values

COMPRESSION_DEFAULT

public static final int COMPRESSION_DEFAULT
The default compression level used. Provides the best balance between speed and compression

See Also:
Constant Field Values

DISABLE_COMPRESSION_ONCE

public static final AttributeKey DISABLE_COMPRESSION_ONCE
A flag that allows you to disable compression once.

Constructor Detail

CompressionFilter

public CompressionFilter()
Creates a new instance which compresses outboud data and decompresses inbound data with default compression level.


CompressionFilter

public CompressionFilter(int compressionLevel)
Creates a new instance which compresses outboud data and decompresses inbound data with the specified compressionLevel.

Parameters:
compressionLevel - the level of compression to be used. Must be one of COMPRESSION_DEFAULT, COMPRESSION_MAX, COMPRESSION_MIN, and COMPRESSION_NONE.

CompressionFilter

public CompressionFilter(boolean compressInbound,
                         boolean compressOutbound,
                         int compressionLevel)
Creates a new instance.

Parameters:
compressInbound - true if data read is to be decompressed
compressOutbound - true if data written is to be compressed
compressionLevel - the level of compression to be used. Must be one of COMPRESSION_DEFAULT, COMPRESSION_MAX, COMPRESSION_MIN, and COMPRESSION_NONE.
Method Detail

messageReceived

public void messageReceived(IoFilter.NextFilter nextFilter,
                            IoSession session,
                            Object message)
                     throws Exception
Description copied from interface: IoFilter
Filters IoHandler.messageReceived(IoSession,Object) event.

Specified by:
messageReceived in interface IoFilter
Overrides:
messageReceived in class IoFilterAdapter
Throws:
Exception

doFilterWrite

protected Object doFilterWrite(IoFilter.NextFilter nextFilter,
                               IoSession session,
                               WriteRequest writeRequest)
                        throws IOException
Specified by:
doFilterWrite in class WriteRequestFilter
Throws:
IOException

onPreAdd

public void onPreAdd(IoFilterChain parent,
                     String name,
                     IoFilter.NextFilter nextFilter)
              throws Exception
Description copied from interface: IoFilter
Invoked before this filter is added to the specified parent. Please note that this method can be invoked more than once if this filter is added to more than one parents. This method is not invoked before IoFilter.init() is invoked.

Specified by:
onPreAdd in interface IoFilter
Overrides:
onPreAdd in class IoFilterAdapter
Parameters:
parent - the parent who called this method
name - the name assigned to this filter
nextFilter - the IoFilter.NextFilter for this filter. You can reuse this object until this filter is removed from the chain.
Throws:
Exception

isCompressInbound

public boolean isCompressInbound()
Returns true if incoming data is being compressed.


setCompressInbound

public void setCompressInbound(boolean compressInbound)
Sets if incoming data has to be compressed.


isCompressOutbound

public boolean isCompressOutbound()
Returns true if the filter is compressing data being written.


setCompressOutbound

public void setCompressOutbound(boolean compressOutbound)
Set if outgoing data has to be compressed.


onPostRemove

public void onPostRemove(IoFilterChain parent,
                         String name,
                         IoFilter.NextFilter nextFilter)
                  throws Exception
Description copied from interface: IoFilter
Invoked after this filter is removed from the specified parent. Please note that this method can be invoked more than once if this filter is removed from more than one parents. This method is always invoked before IoFilter.destroy() is invoked.

Specified by:
onPostRemove in interface IoFilter
Overrides:
onPostRemove in class IoFilterAdapter
Parameters:
parent - the parent who called this method
name - the name assigned to this filter
nextFilter - the IoFilter.NextFilter for this filter. You can reuse this object until this filter is removed from the chain.
Throws:
Exception


Copyright © 2004-2009 Apache MINA Project. All Rights Reserved.