public class CompressionFilter extends IoFilterAdapter
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.
IoFilter.NextFilter
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
protected Object |
doFilterWrite(IoFilter.NextFilter nextFilter,
IoSession session,
WriteRequest writeRequest) |
void |
filterWrite(IoFilter.NextFilter nextFilter,
IoSession session,
WriteRequest writeRequest)
Filters
IoSession.write(Object) method invocation. |
boolean |
isCompressInbound() |
boolean |
isCompressOutbound() |
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.
|
destroy, event, exceptionCaught, filterClose, init, inputClosed, messageSent, onPostAdd, onPreRemove, sessionClosed, sessionCreated, sessionIdle, sessionOpened, toString
public static final int COMPRESSION_MAX
public static final int COMPRESSION_MIN
public static final int COMPRESSION_NONE
public static final int COMPRESSION_DEFAULT
public static final AttributeKey DISABLE_COMPRESSION_ONCE
public CompressionFilter()
public CompressionFilter(int compressionLevel)
compressionLevel
- the level of compression to be used. Must
be one of COMPRESSION_DEFAULT
,
COMPRESSION_MAX
,
COMPRESSION_MIN
, and
COMPRESSION_NONE
.public CompressionFilter(boolean compressInbound, boolean compressOutbound, int compressionLevel)
compressInbound
- true if data read is to be decompressedcompressOutbound
- true if data written is to be compressedcompressionLevel
- the level of compression to be used. Must
be one of COMPRESSION_DEFAULT
,
COMPRESSION_MAX
,
COMPRESSION_MIN
, and
COMPRESSION_NONE
.public void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception
IoSession.write(Object)
method invocation.filterWrite
in interface IoFilter
filterWrite
in class IoFilterAdapter
nextFilter
- the IoFilter.NextFilter
for this filter. You can reuse this
object until this filter is removed from the chain.session
- The IoSession
which has to process this invocationwriteRequest
- The WriteRequest
to processException
- If an error occurred while processing the eventpublic void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message) throws Exception
IoFilterAdapter
IoHandler.messageReceived(IoSession,Object)
event.messageReceived
in interface IoFilter
messageReceived
in class IoFilterAdapter
nextFilter
- the IoFilter.NextFilter
for this filter. You can reuse this
object until this filter is removed from the chain.session
- The IoSession
which has received this eventmessage
- The received messageException
- If an error occurred while processing the eventprotected Object doFilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws IOException
IOException
public void onPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception
IoFilterAdapter
IoFilter.init()
is invoked.onPreAdd
in interface IoFilter
onPreAdd
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.Exception
- If an error occurred while processing the eventpublic boolean isCompressInbound()
public void setCompressInbound(boolean compressInbound)
compressInbound
- true if the incoming data has to be compressedpublic boolean isCompressOutbound()
public void setCompressOutbound(boolean compressOutbound)
compressOutbound
- true if the outgoing data has to be compressedpublic void onPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception
IoFilterAdapter
IoFilter.destroy()
is invoked.onPostRemove
in interface IoFilter
onPostRemove
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.Exception
- If an error occurred while processing the eventCopyright © 2004–2021 Apache MINA Project. All rights reserved.