Class CompressionFilter
- All Implemented Interfaces:
IoFilter
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface IoFilter
IoFilter.NextFilter -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanstatic final intThe default compression level used.static final intMax compression level.static final intProvides the best speed at the price of a low compression ratio.static final intNo compression done on the data.private intprivate booleanprivate final AttributeKeyA session attribute that stores theZlibobject used for compression.static final AttributeKeyA flag that allows you to disable compression once.private final AttributeKeyA session attribute that stores theZlibobject used for decompression. -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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 specifiedcompressionLevel. -
Method Summary
Modifier and TypeMethodDescriptionprotected ObjectdoFilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) voidfilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) FiltersIoSession.write(Object)method invocation.booleanbooleanvoidmessageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message) FiltersIoHandler.messageReceived(IoSession,Object)event.voidonPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) Invoked after this filter is removed from the specifiedparent.voidonPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) Invoked before this filter is added to the specifiedparent.voidsetCompressInbound(boolean compressInbound) Sets if incoming data has to be compressed.voidsetCompressOutbound(boolean compressOutbound) Set if outgoing data has to be compressed.Methods inherited from class IoFilterAdapter
destroy, event, exceptionCaught, filterClose, init, inputClosed, messageSent, onPostAdd, onPreRemove, sessionClosed, sessionCreated, sessionIdle, sessionOpened, toString
-
Field Details
-
COMPRESSION_MAX
public static final int COMPRESSION_MAXMax compression level. Will give the highest compression ratio, but will also take more cpu time and is the slowest.- See Also:
-
COMPRESSION_MIN
public static final int COMPRESSION_MINProvides the best speed at the price of a low compression ratio.- See Also:
-
COMPRESSION_NONE
public static final int COMPRESSION_NONENo compression done on the data.- See Also:
-
COMPRESSION_DEFAULT
public static final int COMPRESSION_DEFAULTThe default compression level used. Provides the best balance between speed and compression- See Also:
-
DEFLATER
A session attribute that stores theZlibobject used for compression. -
INFLATER
A session attribute that stores theZlibobject used for decompression. -
DISABLE_COMPRESSION_ONCE
A flag that allows you to disable compression once. -
compressInbound
private boolean compressInbound -
compressOutbound
private boolean compressOutbound -
compressionLevel
private int compressionLevel
-
-
Constructor Details
-
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 specifiedcompressionLevel.- Parameters:
compressionLevel- the level of compression to be used. Must be one ofCOMPRESSION_DEFAULT,COMPRESSION_MAX,COMPRESSION_MIN, andCOMPRESSION_NONE.
-
CompressionFilter
public CompressionFilter(boolean compressInbound, boolean compressOutbound, int compressionLevel) Creates a new instance.- Parameters:
compressInbound-trueif data read is to be decompressedcompressOutbound-trueif data written is to be compressedcompressionLevel- the level of compression to be used. Must be one ofCOMPRESSION_DEFAULT,COMPRESSION_MAX,COMPRESSION_MIN, andCOMPRESSION_NONE.
-
-
Method Details
-
filterWrite
public void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception FiltersIoSession.write(Object)method invocation.- Specified by:
filterWritein interfaceIoFilter- Overrides:
filterWritein classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has to process this invocationwriteRequest- TheWriteRequestto process- Throws:
Exception- If an error occurred while processing the event
-
messageReceived
public void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message) throws Exception Description copied from class:IoFilterAdapterFiltersIoHandler.messageReceived(IoSession,Object)event.- Specified by:
messageReceivedin interfaceIoFilter- Overrides:
messageReceivedin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this eventmessage- The received message- Throws:
Exception- If an error occurred while processing the event
-
doFilterWrite
protected Object doFilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws IOException - Throws:
IOException
-
onPreAdd
public void onPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception Description copied from class:IoFilterAdapterInvoked before this filter is added to the specifiedparent. 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 beforeIoFilter.init()is invoked.- Specified by:
onPreAddin interfaceIoFilter- Overrides:
onPreAddin classIoFilterAdapter- Parameters:
parent- the parent who called this methodname- the name assigned to this filternextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.- Throws:
Exception- If an error occurred while processing the event
-
isCompressInbound
public boolean isCompressInbound()- Returns:
trueif incoming data is being compressed.
-
setCompressInbound
public void setCompressInbound(boolean compressInbound) Sets if incoming data has to be compressed.- Parameters:
compressInbound-trueif the incoming data has to be compressed
-
isCompressOutbound
public boolean isCompressOutbound()- Returns:
trueif the filter is compressing data being written.
-
setCompressOutbound
public void setCompressOutbound(boolean compressOutbound) Set if outgoing data has to be compressed.- Parameters:
compressOutbound-trueif the outgoing data has to be compressed
-
onPostRemove
public void onPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception Description copied from class:IoFilterAdapterInvoked after this filter is removed from the specifiedparent. 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 beforeIoFilter.destroy()is invoked.- Specified by:
onPostRemovein interfaceIoFilter- Overrides:
onPostRemovein classIoFilterAdapter- Parameters:
parent- the parent who called this methodname- the name assigned to this filternextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.- Throws:
Exception- If an error occurred while processing the event
-