blob: e5f763c687368d6ef927fb37267c952042bb0963 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>5.3 - Compression Filter &mdash; Apache MINA</title>
<link href="/assets/css/common.css" rel="stylesheet" type="text/css"/>
<link href="/assets/css/mina.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<script src="https://www.apachecon.com/event-images/snippet.js"></script>
<div id="container">
<div id="header">
<div id="subProjectsNavBar">
<a href="/">
Apache MINA Project
</a>
&nbsp;|&nbsp;
<a href="/mina-project/">
<strong>MINA</strong>
</a>
&nbsp;|&nbsp;
<a href="/asyncweb-project/">
AsyncWeb
</a>
&nbsp;|&nbsp;
<a href="/ftpserver-project/">
FtpServer
</a>
&nbsp;|&nbsp;
<a href="/sshd-project/">
SSHD
</a>
&nbsp;|&nbsp;
<a href="/vysper-project/">
Vysper
</a>
</div>
</div>
<div id="content">
<div id="leftColumn">
<div id="navigation">
<a class="acevent" data-format="wide" data-width="170"></a>
<h5>Social Networks</h5>
<ul>
<li><a href="https://fosstodon.org/@apachemina">Apache MINA Mastodon</a></li>
</ul>
<h5>Latest Downloads</h5>
<ul>
<li><a href="/mina-project/downloads_2_0.html">Mina 2.0.25</a></li>
<li><a href="/mina-project/downloads_2_1.html">Mina 2.1.8</a></li>
<li><a href="/mina-project/downloads_2_2.html">Mina 2.2.3</a></li>
<li><a href="/mina-project/downloads_old.html">Mina old versions</a></li>
</ul>
<h5>Documentation</h5>
<ul>
<li><a href="/mina-project/documentation.html" class="external-link" rel="nofollow">Base documentation</a></li>
<li><a href="/mina-project/userguide/user-guide-toc.html" class="external-link" rel="nofollow">User guide</a></li>
<li><a href="/mina-project/2.2-vs-2.1.html" class="external-link" rel="nofollow">2.2 vs 2.1</a></li>
<li><a href="/mina-project/2.1-vs-2.0.html" class="external-link" rel="nofollow">2.1 vs 2.0</a></li>
<li><a href="/mina-project/features.html" class="external-link" rel="nofollow">Features</a></li>
<li><a href="/mina-project/road-map.html" class="external-link" rel="nofollow">Road Map</a></li>
<li><a href="/mina-project/quick-start-guide.html" class="external-link" rel="nofollow">Quick Start Guide</a></li>
<li><a href="/mina-project/faq.html" class="external-link" rel="nofollow">FAQ</a></li>
</ul>
<h5>Resources</h5>
<ul>
<li><a href="/mina-project/mailing-lists.html" class="external-link" rel="nofollow">Mailing lists &amp; IRC</a></li>
<li><a href="/mina-project/issue-tracking.html" class="external-link" rel="nofollow">Issue tracking</a></li>
<li><a href="/mina-project/sources.html" class="external-link" rel="nofollow">Sources</a></li>
<li><a href="/mina-project/gen-docs/latest-2.0/apidocs/index.html" class="external-link" rel="nofollow">API Javadoc 2.0.25</a></li>
<li><a href="/mina-project/gen-docs/latest-2.1/apidocs/index.html" class="external-link" rel="nofollow">API Javadoc 2.1.8</a></li>
<li><a href="/mina-project/gen-docs/latest-2.2/apidocs/index.html" class="external-link" rel="nofollow">API Javadoc 2.2.3</a></li>
<li><a href="/mina-project/gen-docs/latest-2.0/xref/index.html" class="external-link" rel="nofollow">API xref 2.0.25</a></li>
<li><a href="/mina-project/gen-docs/latest-2.1/xref/index.html" class="external-link" rel="nofollow">API xref 2.1.8</a></li>
<li><a href="/mina-project/gen-docs/latest-2.2/xref/index.html" class="external-link" rel="nofollow">API xref 2.2.3</a></li>
<li><a href="/mina-project/performances.html" class="external-link" rel="nofollow">Performances</a></li>
<li><a href="/mina-project/testimonials.html" class="external-link" rel="nofollow">Testimonials</a></li>
<li><a href="/mina-project/conferences.html" class="external-link" rel="nofollow">Conferences</a></li>
<li><a href="/mina-project/developer-guide.html" class="external-link" rel="nofollow">Developers Guide</a></li>
<li><a href="/mina-project/related-projects.html" class="external-link" rel="nofollow">Related Projects</a></li>
<li><a href="https://people.apache.org/~vgritsenko/stats/projects/mina.html" class="external-link" rel="nofollow">Statistics</a></li>
</ul>
<h5>Community</h5>
<ul>
<li><a href="https://www.apache.org/foundation/contributing.html" class="external-link" rel="nofollow">Contributing</a></li>
<li><a href="/contributors.html" class="external-link" rel="nofollow">Team</a></li>
<li><a href="/special-thanks.html" class="external-link" rel="nofollow">Special Thanks</a></li>
<li><a href="https://www.apache.org/security/" class="external-link" rel="nofollow">Security</a></li>
</ul>
<h5>About Apache</h5>
<ul>
<li><a href="https://www.apache.org" class="external-link" rel="nofollow">Apache main site</a></li>
<li><a href="https://www.apache.org/licenses/" class="external-link" rel="nofollow">License</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html" title="The ASF sponsorship program" class="external-link" rel="nofollow">Sponsorship program</a></li>
<li><a href="https://www.apache.org/foundation/thanks.html" class="external-link" rel="nofollow">Thanks</a></li>
</ul>
<h3><a name="Navigation-Upcoming"></a>Upcoming</h3>
<ul>
<li>No event</li>
</ul>
</div>
</div>
<div id="rightColumn">
<div class="nav">
<div class="nav_prev">
<a href="ch5.2-buffered-write-filter.html">5.2 - Buffered Write Filter</a>
</div>
<div class="nav_up">
<a href="ch5-filters.html">Chapter 5 - Filters</a>
</div>
<div class="nav_next">
<a href="ch5.4-connection-throttle-filter.html">5.4 - Connection Throttle Filter</a>
</div>
<div class="clearfix"></div>
</div>
<h1 id="53---compression-filter">5.3 - Compression Filter</h1>
<p>The <em>CompressionFilter</em> class is used to compress data before it gets sent and decompress it when it&rsquo;s received. It uses the <a href="http://www.jcraft.com/jzlib/">JZLIB library</a>.</p>
<p>It only handles two events :</p>
<ul>
<li><em>messageReceived</em>: The received message will be decompressed, if it&rsquo;s contained in a <em>IoBuffer</em>.</li>
<li><em>filterWrite</em>: The message to write will be compressed, assuming it&rsquo;s stored in a <em>IoBuffer</em> (otherwise an exception will be generated).</li>
</ul>
<h2 id="configuration">Configuration</h2>
<p>It&rsquo;s possible to configure the <em>CompressionFilter</em>, with the listed parameters :</p>
<ul>
<li><em>compressInbound</em>: if set to <em>true</em>, the incoming data will be decompressed, otherwise they will be left intact.</li>
<li><em>compressOutbound</em>: if set to <em>true</em>, the outgoing data will be compressed, otherwise they will be ignored.</li>
<li><em>compressionLevel</em>: one of <em>COMPRESSION_DEFAULT</em> (-1), <em>COMPRESSION_MAX</em> (9), <em>COMPRESSION_MIN</em> (1), <em>COMPRESSION_NONE</em> (0). Sets the level, of desired compression.</li>
</ul>
<p>There is one more parameter that can be used, injected into the session&rsquo;s attributes, the <em>DISABLE_COMPRESSION_ONCE</em> flag. If present in the session&rsquo;s attributes, the first compression will be ignored (this is probably useful for application that needs to send a message that the flowing messages are going to be compressed).</p>
<h2 id="initialization">Initialization</h2>
<p>When the filter is added to the chain, instances of the deflater and inflater are created and injected into the session&rsquo;s attributes. This could change, as each session has its own instance of the filter in its chain, so there is no reason not to store those instances in the filter itself.</p>
<h2 id="side-notes">Side notes</h2>
<p>The way this filter works does not make it very efficient. It requires that the received data to be decompressed are fully read - and there are no control that it&rsquo;s the case of not -, and it also requires that the data to be compressed be fully compressed in one pass, which is quite inefficient from the memory perspective, as we may compress data by blocks, and send them immediately, waiting for the block to be really sent to compress the next one.</p>
<p>One more thing: there is no way this filter can be used to compress something like a file or anything that is not contained in a <em>IoFilter</em>. This is a pretty strong limitation.</p>
<p>All in all, it&rsquo;s a brittle filter, you&rsquo;d better not use it as it is!</p>
<h2 id="future-improvements">Future improvements</h2>
<p>So we need a compression filter that does not eat all the memory, possibly a stateless one - why should we have to create as many instances as we have sessions ? -, that is fast, AL 2.0 compatible, and that can flush data pieces by pieces.</p>
<h3 id="stateless-compressiondecompression">Stateless compression/decompression</h3>
<p>The idea is to have a function that can be called statically, with a context. It&rsquo;s not necessarily complex we just need to keep track of a context.</p>
<p>It should be possible top compress a block of data, send it, and process the next block when the first block has been processed. Same thing with received data: it should be possible to process a buffer, decompress it and wait for the remaining bytes until a block of data has been fully decompressed.</p>
<div class="nav">
<div class="nav_prev">
<a href="ch5.2-buffered-write-filter.html">5.2 - Buffered Write Filter</a>
</div>
<div class="nav_up">
<a href="ch5-filters.html">Chapter 5 - Filters</a>
</div>
<div class="nav_next">
<a href="ch5.4-connection-throttle-filter.html">5.4 - Connection Throttle Filter</a>
</div>
<div class="clearfix"></div>
</div>
</div>
<div id="endContent"></div>
</div>
<div id="footer">
&copy; 2003-2024, <a href="https://www.apache.org">The Apache Software Foundation</a> - <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy Policy</a><br />
Apache MINA, MINA, Apache Vysper, Vysper, Apache SSHd, SSHd, Apache FtpServer, FtpServer, Apache AsyncWeb, AsyncWeb,
Apache, the Apache feather logo, and the Apache Mina project logos are trademarks of The Apache Software Foundation.
</div>
</div>
</body>
</html>