blob: 59b2ee43819c217745d8faafb14d56f892edf428 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 9 - Codec 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="../ch8-iobuffer/ch8-iobuffer.html">Chapter 8 - IoBuffer</a>
</div>
<div class="nav_up">
<a href="../user-guide-toc.html">User Guide</a>
</div>
<div class="nav_next">
<a href="../ch10-executor-filter/ch10-executor-filter.html">Chapter 10 - Executor Filter</a>
</div>
<div class="clearfix"></div>
</div>
<h1 id="chapter-9---codec-filter">Chapter 9 - Codec Filter</h1>
<p>This tutorial tries to explain why and how to use a ProtocolCodecFilter.</p>
<h2 id="why-use-a-protocolcodecfilter">Why use a ProtocolCodecFilter?</h2>
<ul>
<li>TCP guarantees delivery of all packets in the correct order.
But there is no guarantee that one write operation on the sender-side will result in one read event on the receiving side.
see <a href="http://en.wikipedia.org/wiki/IPv4#Fragmentation_and_reassembly">http://en.wikipedia.org/wiki/IPv4#Fragmentation_and_reassembly</a> and <a href="http://en.wikipedia.org/wiki/Nagle%27s_algorithm">http://en.wikipedia.org/wiki/Nagle%27s_algorithm</a>
In MINA terminology: without a ProtocolCodecFilter one call of IoSession.write(Object message) by the sender can result in multiple messageReceived(IoSession session, Object message) events on the receiver; and multiple calls of IoSession.write(Object message) can lead to a single messageReceived event. You might not encounter this behavior when client and server are running on the same host (or an a local network) but your applications should be able to cope with this.</li>
<li>Most network applications need a way to find out where the current message ends and where the next message starts.</li>
<li>You could implement all this logic in your IoHandler, but adding a ProtocolCodecFilter will make your code much cleaner and easier to maintain.</li>
<li>It allows you to separate your protocol logic from your business logic (IoHandler).</li>
</ul>
<h2 id="how-">How ?</h2>
<p>Your application is basically just receiving a bunch of bytes and you need to convert these bytes into messages (higher level objects).</p>
<p>There are three common techniques for splitting the stream of bytes into messages:</p>
<ul>
<li>use fixed length messages</li>
<li>use a fixed length header that indicates the length of the body</li>
<li>using a delimiter; for example many text-based protocols append a newline (or CR LF pair) after every message (<a href="http://www.faqs.org/rfcs/rfc977.html">http://www.faqs.org/rfcs/rfc977.html</a>)</li>
</ul>
<p>In this tutorial we will use the first and second method since they are definitely easier to implement. Afterwards we will look at using a delimiter.</p>
<h2 id="example">Example</h2>
<p>We will develop a (pretty useless) graphical chargen server to illustrate how to implement your own protocol codec (ProtocolEncoder, ProtocolDecoder, and ProtocolCodecFactory).
The protocol is really simple. This is the layout of a request message:</p>
<table>
<thead>
<tr>
<th>4 bytes</th>
<th>4 bytes</th>
<th>4 bytes</th>
</tr>
</thead>
<tbody>
<tr>
<td>width</td>
<td>height</td>
<td>numchars</td>
</tr>
</tbody>
</table>
<ul>
<li>width: the width of the requested image (an integer in network byte-order)</li>
<li>height: the height of the requested image (an integer in network byte-order)</li>
<li>numchars: the number of chars to generate (an integer in network byte-order)</li>
</ul>
<p>The server responds with two images of the requested dimensions, with the requested number of characters painted on it.
This is the layout of a response message:</p>
<table>
<thead>
<tr>
<th>4 bytes</th>
<th>variable length body</th>
<th>4 bytes</th>
<th>variable length body</th>
</tr>
</thead>
<tbody>
<tr>
<td>length1</td>
<td>image1</td>
<td>length2</td>
<td>image2</td>
</tr>
</tbody>
</table>
<p>Overview of the classes we need for encoding and decoding requests and responses:</p>
<ul>
<li><strong>ImageRequest</strong>: a simple POJO representing a request to our ImageServer.</li>
<li><strong>ImageRequestEncoder</strong>: encodes ImageRequest objects into protocol-specific data (used by the client)</li>
<li><strong>ImageRequestDecoder</strong>: decodes protocol-specific data into ImageRequest objects (used by the server)</li>
<li><strong>ImageResponse</strong>: a simple POJO representing a response from our ImageServer.</li>
<li><strong>ImageResponseEncoder</strong>: used by the server for encoding ImageResponse objects</li>
<li><strong>ImageResponseDecoder</strong>: used by the client for decoding ImageResponse objects</li>
<li><strong>ImageCodecFactory</strong>: this class creates the necessary encoders and decoders</li>
</ul>
<p>Here is the ImageRequest class :</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageRequest</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">private</span> <span style="color:#0b0;font-weight:bold">int</span> width<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> <span style="color:#0b0;font-weight:bold">int</span> height<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> <span style="color:#0b0;font-weight:bold">int</span> numberOfCharacters<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#00a000">ImageRequest</span><span style="color:#666">(</span><span style="color:#0b0;font-weight:bold">int</span> width<span style="color:#666">,</span> <span style="color:#0b0;font-weight:bold">int</span> height<span style="color:#666">,</span> <span style="color:#0b0;font-weight:bold">int</span> numberOfCharacters<span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">width</span> <span style="color:#666">=</span> width<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">height</span> <span style="color:#666">=</span> height<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">numberOfCharacters</span> <span style="color:#666">=</span> numberOfCharacters<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">int</span> <span style="color:#00a000">getWidth</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> width<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">int</span> <span style="color:#00a000">getHeight</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> height<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">int</span> <span style="color:#00a000">getNumberOfCharacters</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> numberOfCharacters<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Encoding is usually simpler than decoding, so let&rsquo;s start with the ImageRequestEncoder:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageRequestEncoder</span> <span style="color:#a2f;font-weight:bold">implements</span> ProtocolEncoder <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">encode</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> Object message<span style="color:#666">,</span> ProtocolEncoderOutput out<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
ImageRequest request <span style="color:#666">=</span> <span style="color:#666">(</span>ImageRequest<span style="color:#666">)</span> message<span style="color:#666">;</span>
IoBuffer buffer <span style="color:#666">=</span> IoBuffer<span style="color:#666">.</span><span style="color:#b44">allocate</span><span style="color:#666">(</span>12<span style="color:#666">,</span> <span style="color:#a2f;font-weight:bold">false</span><span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">putInt</span><span style="color:#666">(</span>request<span style="color:#666">.</span><span style="color:#b44">getWidth</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">putInt</span><span style="color:#666">(</span>request<span style="color:#666">.</span><span style="color:#b44">getHeight</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">putInt</span><span style="color:#666">(</span>request<span style="color:#666">.</span><span style="color:#b44">getNumberOfCharacters</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">flip</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
out<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>buffer<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">dispose</span><span style="color:#666">(</span>IoSession session<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
<span style="color:#080;font-style:italic">// nothing to dispose
</span><span style="color:#080;font-style:italic"></span> <span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Remarks:</p>
<ul>
<li>MINA will call the encode function for all messages in the IoSession&rsquo;s write queue. Since our client will only write ImageRequest objects, we can safely cast message to ImageRequest.</li>
<li>We allocate a new IoBuffer from the heap. It&rsquo;s best to avoid using direct buffers, since generally heap buffers perform better.
see <a href="http://issues.apache.org/jira/browse/DIRMINA-289">http://issues.apache.org/jira/browse/DIRMINA-289</a>)</li>
<li>You do not have to release the buffer, MINA will do it for you, see <a href="https://nightlies.apache.org/mina/mina/2.0.22/apidocs/org/apache/mina/core/buffer/IoBuffer.html">https://nightlies.apache.org/mina/mina/2.0.22/apidocs/org/apache/mina/core/buffer/IoBuffer.html</a></li>
<li>In the dispose() method you should release all resources acquired during encoding for the specified session. If there is nothing to dispose you could let your encoder inherit from ProtocolEncoderAdapter.</li>
</ul>
<p>Now let&rsquo;s have a look at the decoder. The CumulativeProtocolDecoder is a great help for writing your own decoder: it will buffer all incoming data until your decoder decides it can do something with it.
In this case the message has a fixed size, so it&rsquo;s easiest to wait until all data is available:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageRequestDecoder</span> <span style="color:#a2f;font-weight:bold">extends</span> CumulativeProtocolDecoder <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">protected</span> <span style="color:#0b0;font-weight:bold">boolean</span> <span style="color:#00a000">doDecode</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> IoBuffer in<span style="color:#666">,</span> ProtocolDecoderOutput out<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>in<span style="color:#666">.</span><span style="color:#b44">remaining</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">&gt;</span><span style="color:#666">=</span> 12<span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#0b0;font-weight:bold">int</span> width <span style="color:#666">=</span> in<span style="color:#666">.</span><span style="color:#b44">getInt</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#0b0;font-weight:bold">int</span> height <span style="color:#666">=</span> in<span style="color:#666">.</span><span style="color:#b44">getInt</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#0b0;font-weight:bold">int</span> numberOfCharachters <span style="color:#666">=</span> in<span style="color:#666">.</span><span style="color:#b44">getInt</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
ImageRequest request <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ImageRequest<span style="color:#666">(</span>width<span style="color:#666">,</span> height<span style="color:#666">,</span> numberOfCharachters<span style="color:#666">)</span><span style="color:#666">;</span>
out<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>request<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">return</span> <span style="color:#a2f;font-weight:bold">true</span><span style="color:#666">;</span>
<span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> <span style="color:#a2f;font-weight:bold">false</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Remarks:</p>
<ul>
<li>every time a complete message is decoded, you should write it to the ProtocolDecoderOutput; these messages will travel along the filter-chain and eventually arrive in your IoHandler.messageReceived method</li>
<li>you are not responsible for releasing the IoBuffer</li>
<li>when there is not enough data available to decode a message, just return false</li>
</ul>
<p>The response is also a very simple POJO:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageResponse</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">private</span> BufferedImage image1<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> BufferedImage image2<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#00a000">ImageResponse</span><span style="color:#666">(</span>BufferedImage image1<span style="color:#666">,</span> BufferedImage image2<span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">image1</span> <span style="color:#666">=</span> image1<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">image2</span> <span style="color:#666">=</span> image2<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> BufferedImage <span style="color:#00a000">getImage1</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> image1<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> BufferedImage <span style="color:#00a000">getImage2</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> image2<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Encoding the response is also trivial:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageResponseEncoder</span> <span style="color:#a2f;font-weight:bold">extends</span> ProtocolEncoderAdapter <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">encode</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> Object message<span style="color:#666">,</span> ProtocolEncoderOutput out<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
ImageResponse imageResponse <span style="color:#666">=</span> <span style="color:#666">(</span>ImageResponse<span style="color:#666">)</span> message<span style="color:#666">;</span>
<span style="color:#0b0;font-weight:bold">byte</span><span style="color:#666">[</span><span style="color:#666">]</span> bytes1 <span style="color:#666">=</span> getBytes<span style="color:#666">(</span>imageResponse<span style="color:#666">.</span><span style="color:#b44">getImage1</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#0b0;font-weight:bold">byte</span><span style="color:#666">[</span><span style="color:#666">]</span> bytes2 <span style="color:#666">=</span> getBytes<span style="color:#666">(</span>imageResponse<span style="color:#666">.</span><span style="color:#b44">getImage2</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#0b0;font-weight:bold">int</span> capacity <span style="color:#666">=</span> bytes1<span style="color:#666">.</span><span style="color:#b44">length</span> <span style="color:#666">+</span> bytes2<span style="color:#666">.</span><span style="color:#b44">length</span> <span style="color:#666">+</span> 8<span style="color:#666">;</span>
IoBuffer buffer <span style="color:#666">=</span> IoBuffer<span style="color:#666">.</span><span style="color:#b44">allocate</span><span style="color:#666">(</span>capacity<span style="color:#666">,</span> <span style="color:#a2f;font-weight:bold">false</span><span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">setAutoExpand</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">true</span><span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">putInt</span><span style="color:#666">(</span>bytes1<span style="color:#666">.</span><span style="color:#b44">length</span><span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">put</span><span style="color:#666">(</span>bytes1<span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">putInt</span><span style="color:#666">(</span>bytes2<span style="color:#666">.</span><span style="color:#b44">length</span><span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">put</span><span style="color:#666">(</span>bytes2<span style="color:#666">)</span><span style="color:#666">;</span>
buffer<span style="color:#666">.</span><span style="color:#b44">flip</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
out<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>buffer<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">private</span> <span style="color:#0b0;font-weight:bold">byte</span><span style="color:#666">[</span><span style="color:#666">]</span> <span style="color:#00a000">getBytes</span><span style="color:#666">(</span>BufferedImage image<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> IOException <span style="color:#666">{</span>
ByteArrayOutputStream baos <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ByteArrayOutputStream<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
ImageIO<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>image<span style="color:#666">,</span> <span style="color:#b44">&#34;PNG&#34;</span><span style="color:#666">,</span> baos<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">return</span> baos<span style="color:#666">.</span><span style="color:#b44">toByteArray</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Remarks:</p>
<ul>
<li>when it is impossible to calculate the length of the IoBuffer beforehand, you can use an auto-expanding buffer by calling buffer.setAutoExpand(true);</li>
</ul>
<p>Now let&rsquo;s have a look at decoding the response:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageResponseDecoder</span> <span style="color:#a2f;font-weight:bold">extends</span> CumulativeProtocolDecoder <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">private</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#a2f;font-weight:bold">final</span> String DECODER_STATE_KEY <span style="color:#666">=</span> ImageResponseDecoder<span style="color:#666">.</span><span style="color:#b44">class</span><span style="color:#666">.</span><span style="color:#b44">getName</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">+</span> <span style="color:#b44">&#34;.STATE&#34;</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#a2f;font-weight:bold">final</span> <span style="color:#0b0;font-weight:bold">int</span> MAX_IMAGE_SIZE <span style="color:#666">=</span> 5 <span style="color:#666">*</span> 1024 <span style="color:#666">*</span> 1024<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">DecoderState</span> <span style="color:#666">{</span>
BufferedImage image1<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">protected</span> <span style="color:#0b0;font-weight:bold">boolean</span> <span style="color:#00a000">doDecode</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> IoBuffer in<span style="color:#666">,</span> ProtocolDecoderOutput out<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
DecoderState decoderState <span style="color:#666">=</span> <span style="color:#666">(</span>DecoderState<span style="color:#666">)</span> session<span style="color:#666">.</span><span style="color:#b44">getAttribute</span><span style="color:#666">(</span>DECODER_STATE_KEY<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>decoderState <span style="color:#666">=</span><span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">)</span> <span style="color:#666">{</span>
decoderState <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> DecoderState<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
session<span style="color:#666">.</span><span style="color:#b44">setAttribute</span><span style="color:#666">(</span>DECODER_STATE_KEY<span style="color:#666">,</span> decoderState<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>decoderState<span style="color:#666">.</span><span style="color:#b44">image1</span> <span style="color:#666">=</span><span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#080;font-style:italic">// try to read first image
</span><span style="color:#080;font-style:italic"></span> <span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>in<span style="color:#666">.</span><span style="color:#b44">prefixedDataAvailable</span><span style="color:#666">(</span>4<span style="color:#666">,</span> MAX_IMAGE_SIZE<span style="color:#666">)</span><span style="color:#666">)</span> <span style="color:#666">{</span>
decoderState<span style="color:#666">.</span><span style="color:#b44">image1</span> <span style="color:#666">=</span> readImage<span style="color:#666">(</span>in<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
<span style="color:#080;font-style:italic">// not enough data available to read first image
</span><span style="color:#080;font-style:italic"></span> <span style="color:#a2f;font-weight:bold">return</span> <span style="color:#a2f;font-weight:bold">false</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>decoderState<span style="color:#666">.</span><span style="color:#b44">image1</span> <span style="color:#666">!</span><span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#080;font-style:italic">// try to read second image
</span><span style="color:#080;font-style:italic"></span> <span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>in<span style="color:#666">.</span><span style="color:#b44">prefixedDataAvailable</span><span style="color:#666">(</span>4<span style="color:#666">,</span> MAX_IMAGE_SIZE<span style="color:#666">)</span><span style="color:#666">)</span> <span style="color:#666">{</span>
BufferedImage image2 <span style="color:#666">=</span> readImage<span style="color:#666">(</span>in<span style="color:#666">)</span><span style="color:#666">;</span>
ImageResponse imageResponse <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ImageResponse<span style="color:#666">(</span>decoderState<span style="color:#666">.</span><span style="color:#b44">image1</span><span style="color:#666">,</span> image2<span style="color:#666">)</span><span style="color:#666">;</span>
out<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>imageResponse<span style="color:#666">)</span><span style="color:#666">;</span>
decoderState<span style="color:#666">.</span><span style="color:#b44">image1</span> <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">null</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">return</span> <span style="color:#a2f;font-weight:bold">true</span><span style="color:#666">;</span>
<span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
<span style="color:#080;font-style:italic">// not enough data available to read second image
</span><span style="color:#080;font-style:italic"></span> <span style="color:#a2f;font-weight:bold">return</span> <span style="color:#a2f;font-weight:bold">false</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">return</span> <span style="color:#a2f;font-weight:bold">false</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">private</span> BufferedImage <span style="color:#00a000">readImage</span><span style="color:#666">(</span>IoBuffer in<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> IOException <span style="color:#666">{</span>
<span style="color:#0b0;font-weight:bold">int</span> length <span style="color:#666">=</span> in<span style="color:#666">.</span><span style="color:#b44">getInt</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#0b0;font-weight:bold">byte</span><span style="color:#666">[</span><span style="color:#666">]</span> bytes <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> <span style="color:#0b0;font-weight:bold">byte</span><span style="color:#666">[</span>length<span style="color:#666">]</span><span style="color:#666">;</span>
in<span style="color:#666">.</span><span style="color:#b44">get</span><span style="color:#666">(</span>bytes<span style="color:#666">)</span><span style="color:#666">;</span>
ByteArrayInputStream bais <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ByteArrayInputStream<span style="color:#666">(</span>bytes<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">return</span> ImageIO<span style="color:#666">.</span><span style="color:#b44">read</span><span style="color:#666">(</span>bais<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Remarks:</p>
<ul>
<li>We store the state of the decoding process in a session attribute. It would also be possible to store this state in the Decoder object itself but this has several disadvantages:
<ul>
<li>every IoSession would need its own Decoder instance</li>
<li>MINA ensures that there will never be more than one thread simultaneously executing the decode() function for the same IoSession, but it does not guarantee that it will always be the same thread. Suppose the first piece of data is handled by thread-1 who decides it cannot yet decode, when the next piece of data arrives, it could be handled by another thread. To avoid visibility problems, you must properly synchronize access to this decoder state (IoSession attributes are stored in a ConcurrentHashMap, so they are automatically visible to other threads).</li>
<li>a discussion on the mailing list has lead to this conclusion: choosing between storing state in the IoSession or in the Decoder instance itself is more a matter of taste. To ensure that no two threads will run the decode method for the same IoSession, MINA needs to do some form of synchronization =&gt; this synchronization will also ensure you can&rsquo;t have the visibility problem described above.
(Thanks to Adam Fisk for pointing this out)
see <a href="https://www.mail-archive.com/dev@mina.apache.org/msg03038.html">https://www.mail-archive.com/dev@mina.apache.org/msg03038.html</a></li>
</ul>
</li>
<li>IoBuffer.prefixedDataAvailable() is very convenient when your protocol uses a length-prefix; it supports a prefix of 1, 2 or 4 bytes.</li>
<li>don&rsquo;t forget to reset the decoder state when you&rsquo;ve decoded a response (removing the session attribute is another way to do it)</li>
</ul>
<p>If the response would consist of a single image, we would not need to store decoder state:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">protected</span> <span style="color:#0b0;font-weight:bold">boolean</span> <span style="color:#00a000">doDecode</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> IoBuffer in<span style="color:#666">,</span> ProtocolDecoderOutput out<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>in<span style="color:#666">.</span><span style="color:#b44">prefixedDataAvailable</span><span style="color:#666">(</span>4<span style="color:#666">)</span><span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#0b0;font-weight:bold">int</span> length <span style="color:#666">=</span> in<span style="color:#666">.</span><span style="color:#b44">getInt</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#0b0;font-weight:bold">byte</span><span style="color:#666">[</span><span style="color:#666">]</span> bytes <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> <span style="color:#0b0;font-weight:bold">byte</span><span style="color:#666">[</span>length<span style="color:#666">]</span><span style="color:#666">;</span>
in<span style="color:#666">.</span><span style="color:#b44">get</span><span style="color:#666">(</span>bytes<span style="color:#666">)</span><span style="color:#666">;</span>
ByteArrayInputStream bais <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ByteArrayInputStream<span style="color:#666">(</span>bytes<span style="color:#666">)</span><span style="color:#666">;</span>
BufferedImage image <span style="color:#666">=</span> ImageIO<span style="color:#666">.</span><span style="color:#b44">read</span><span style="color:#666">(</span>bais<span style="color:#666">)</span><span style="color:#666">;</span>
out<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>image<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">return</span> <span style="color:#a2f;font-weight:bold">true</span><span style="color:#666">;</span>
<span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> <span style="color:#a2f;font-weight:bold">false</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Now let&rsquo;s glue it all together:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageCodecFactory</span> <span style="color:#a2f;font-weight:bold">implements</span> ProtocolCodecFactory <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">private</span> ProtocolEncoder encoder<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> ProtocolDecoder decoder<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#00a000">ImageCodecFactory</span><span style="color:#666">(</span><span style="color:#0b0;font-weight:bold">boolean</span> client<span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>client<span style="color:#666">)</span> <span style="color:#666">{</span>
encoder <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ImageRequestEncoder<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
decoder <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ImageResponseDecoder<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">else</span> <span style="color:#666">{</span>
encoder <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ImageResponseEncoder<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
decoder <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ImageRequestDecoder<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> ProtocolEncoder <span style="color:#00a000">getEncoder</span><span style="color:#666">(</span>IoSession ioSession<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> encoder<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> ProtocolDecoder <span style="color:#00a000">getDecoder</span><span style="color:#666">(</span>IoSession ioSession<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">return</span> decoder<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Remarks:</p>
<ul>
<li>for every new session, MINA will ask the ImageCodecFactory for an encoder and a decoder.</li>
<li>since our encoders and decoders store no conversational state, it is safe to let all sessions share a single instance.</li>
</ul>
<p>This is how the server would use the ProtocolCodecFactory:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageServer</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#a2f;font-weight:bold">final</span> <span style="color:#0b0;font-weight:bold">int</span> PORT <span style="color:#666">=</span> 33789<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">main</span><span style="color:#666">(</span>String<span style="color:#666">[</span><span style="color:#666">]</span> args<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> IOException <span style="color:#666">{</span>
ImageServerIoHandler handler <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ImageServerIoHandler<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
NioSocketAcceptor acceptor <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> NioSocketAcceptor<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
acceptor<span style="color:#666">.</span><span style="color:#b44">getFilterChain</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">.</span><span style="color:#b44">addLast</span><span style="color:#666">(</span><span style="color:#b44">&#34;protocol&#34;</span><span style="color:#666">,</span> <span style="color:#a2f;font-weight:bold">new</span> ProtocolCodecFilter<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> ImageCodecFactory<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">false</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
acceptor<span style="color:#666">.</span><span style="color:#b44">setLocalAddress</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> InetSocketAddress<span style="color:#666">(</span>PORT<span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
acceptor<span style="color:#666">.</span><span style="color:#b44">setHandler</span><span style="color:#666">(</span>handler<span style="color:#666">)</span><span style="color:#666">;</span>
acceptor<span style="color:#666">.</span><span style="color:#b44">bind</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;server is listenig at port &#34;</span> <span style="color:#666">+</span> PORT<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p>Usage by the client is identical:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageClient</span> <span style="color:#a2f;font-weight:bold">extends</span> IoHandlerAdapter <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#a2f;font-weight:bold">final</span> <span style="color:#0b0;font-weight:bold">int</span> CONNECT_TIMEOUT <span style="color:#666">=</span> 3000<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> String host<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> <span style="color:#0b0;font-weight:bold">int</span> port<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> SocketConnector connector<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> IoSession session<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> ImageListener imageListener<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#00a000">ImageClient</span><span style="color:#666">(</span>String host<span style="color:#666">,</span> <span style="color:#0b0;font-weight:bold">int</span> port<span style="color:#666">,</span> ImageListener imageListener<span style="color:#666">)</span> <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">host</span> <span style="color:#666">=</span> host<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">port</span> <span style="color:#666">=</span> port<span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">imageListener</span> <span style="color:#666">=</span> imageListener<span style="color:#666">;</span>
connector <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> NioSocketConnector<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
connector<span style="color:#666">.</span><span style="color:#b44">getFilterChain</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">.</span><span style="color:#b44">addLast</span><span style="color:#666">(</span><span style="color:#b44">&#34;codec&#34;</span><span style="color:#666">,</span> <span style="color:#a2f;font-weight:bold">new</span> ProtocolCodecFilter<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> ImageCodecFactory<span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">true</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
connector<span style="color:#666">.</span><span style="color:#b44">setHandler</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">messageReceived</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> Object message<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
ImageResponse response <span style="color:#666">=</span> <span style="color:#666">(</span>ImageResponse<span style="color:#666">)</span> message<span style="color:#666">;</span>
imageListener<span style="color:#666">.</span><span style="color:#b44">onImages</span><span style="color:#666">(</span>response<span style="color:#666">.</span><span style="color:#b44">getImage1</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">,</span> response<span style="color:#666">.</span><span style="color:#b44">getImage2</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">.</span><span style="color:#666">.</span><span style="color:#666">.</span>
</code></pre></div><p>For completeness, I will add the code for the server-side IoHandler:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">class</span> <span style="color:#00f">ImageServerIoHandler</span> <span style="color:#a2f;font-weight:bold">extends</span> IoHandlerAdapter <span style="color:#666">{</span>
<span style="color:#a2f;font-weight:bold">private</span> <span style="color:#a2f;font-weight:bold">final</span> <span style="color:#a2f;font-weight:bold">static</span> String characters <span style="color:#666">=</span> <span style="color:#b44">&#34;mina rocks abcdefghijklmnopqrstuvwxyz0123456789&#34;</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#a2f;font-weight:bold">static</span> <span style="color:#a2f;font-weight:bold">final</span> String INDEX_KEY <span style="color:#666">=</span> ImageServerIoHandler<span style="color:#666">.</span><span style="color:#b44">class</span><span style="color:#666">.</span><span style="color:#b44">getName</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">+</span> <span style="color:#b44">&#34;.INDEX&#34;</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">private</span> Logger logger <span style="color:#666">=</span> LoggerFactory<span style="color:#666">.</span><span style="color:#b44">getLogger</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">this</span><span style="color:#666">.</span><span style="color:#b44">getClass</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">sessionOpened</span><span style="color:#666">(</span>IoSession session<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
session<span style="color:#666">.</span><span style="color:#b44">setAttribute</span><span style="color:#666">(</span>INDEX_KEY<span style="color:#666">,</span> 0<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">exceptionCaught</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> Throwable cause<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
IoSessionLogger sessionLogger <span style="color:#666">=</span> IoSessionLogger<span style="color:#666">.</span><span style="color:#b44">getLogger</span><span style="color:#666">(</span>session<span style="color:#666">,</span> logger<span style="color:#666">)</span><span style="color:#666">;</span>
sessionLogger<span style="color:#666">.</span><span style="color:#b44">warn</span><span style="color:#666">(</span>cause<span style="color:#666">.</span><span style="color:#b44">getMessage</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">,</span> cause<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">public</span> <span style="color:#0b0;font-weight:bold">void</span> <span style="color:#00a000">messageReceived</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> Object message<span style="color:#666">)</span> <span style="color:#a2f;font-weight:bold">throws</span> Exception <span style="color:#666">{</span>
ImageRequest request <span style="color:#666">=</span> <span style="color:#666">(</span>ImageRequest<span style="color:#666">)</span> message<span style="color:#666">;</span>
String text1 <span style="color:#666">=</span> generateString<span style="color:#666">(</span>session<span style="color:#666">,</span> request<span style="color:#666">.</span><span style="color:#b44">getNumberOfCharacters</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
String text2 <span style="color:#666">=</span> generateString<span style="color:#666">(</span>session<span style="color:#666">,</span> request<span style="color:#666">.</span><span style="color:#b44">getNumberOfCharacters</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
BufferedImage image1 <span style="color:#666">=</span> createImage<span style="color:#666">(</span>request<span style="color:#666">,</span> text1<span style="color:#666">)</span><span style="color:#666">;</span>
BufferedImage image2 <span style="color:#666">=</span> createImage<span style="color:#666">(</span>request<span style="color:#666">,</span> text2<span style="color:#666">)</span><span style="color:#666">;</span>
ImageResponse response <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> ImageResponse<span style="color:#666">(</span>image1<span style="color:#666">,</span> image2<span style="color:#666">)</span><span style="color:#666">;</span>
session<span style="color:#666">.</span><span style="color:#b44">write</span><span style="color:#666">(</span>response<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">private</span> BufferedImage <span style="color:#00a000">createImage</span><span style="color:#666">(</span>ImageRequest request<span style="color:#666">,</span> String text<span style="color:#666">)</span> <span style="color:#666">{</span>
BufferedImage image <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> BufferedImage<span style="color:#666">(</span>request<span style="color:#666">.</span><span style="color:#b44">getWidth</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">,</span> request<span style="color:#666">.</span><span style="color:#b44">getHeight</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">,</span> BufferedImage<span style="color:#666">.</span><span style="color:#b44">TYPE_BYTE_INDEXED</span><span style="color:#666">)</span><span style="color:#666">;</span>
Graphics graphics <span style="color:#666">=</span> image<span style="color:#666">.</span><span style="color:#b44">createGraphics</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
graphics<span style="color:#666">.</span><span style="color:#b44">setColor</span><span style="color:#666">(</span>Color<span style="color:#666">.</span><span style="color:#b44">YELLOW</span><span style="color:#666">)</span><span style="color:#666">;</span>
graphics<span style="color:#666">.</span><span style="color:#b44">fillRect</span><span style="color:#666">(</span>0<span style="color:#666">,</span> 0<span style="color:#666">,</span> image<span style="color:#666">.</span><span style="color:#b44">getWidth</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">,</span> image<span style="color:#666">.</span><span style="color:#b44">getHeight</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
Font serif <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> Font<span style="color:#666">(</span><span style="color:#b44">&#34;serif&#34;</span><span style="color:#666">,</span> Font<span style="color:#666">.</span><span style="color:#b44">PLAIN</span><span style="color:#666">,</span> 30<span style="color:#666">)</span><span style="color:#666">;</span>
graphics<span style="color:#666">.</span><span style="color:#b44">setFont</span><span style="color:#666">(</span>serif<span style="color:#666">)</span><span style="color:#666">;</span>
graphics<span style="color:#666">.</span><span style="color:#b44">setColor</span><span style="color:#666">(</span>Color<span style="color:#666">.</span><span style="color:#b44">BLUE</span><span style="color:#666">)</span><span style="color:#666">;</span>
graphics<span style="color:#666">.</span><span style="color:#b44">drawString</span><span style="color:#666">(</span>text<span style="color:#666">,</span> 10<span style="color:#666">,</span> 50<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">return</span> image<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#a2f;font-weight:bold">private</span> String <span style="color:#00a000">generateString</span><span style="color:#666">(</span>IoSession session<span style="color:#666">,</span> <span style="color:#0b0;font-weight:bold">int</span> length<span style="color:#666">)</span> <span style="color:#666">{</span>
Integer index <span style="color:#666">=</span> <span style="color:#666">(</span>Integer<span style="color:#666">)</span> session<span style="color:#666">.</span><span style="color:#b44">getAttribute</span><span style="color:#666">(</span>INDEX_KEY<span style="color:#666">)</span><span style="color:#666">;</span>
StringBuffer buffer <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> StringBuffer<span style="color:#666">(</span>length<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">while</span> <span style="color:#666">(</span>buffer<span style="color:#666">.</span><span style="color:#b44">length</span><span style="color:#666">(</span><span style="color:#666">)</span> <span style="color:#666">&lt;</span> length<span style="color:#666">)</span> <span style="color:#666">{</span>
buffer<span style="color:#666">.</span><span style="color:#b44">append</span><span style="color:#666">(</span>characters<span style="color:#666">.</span><span style="color:#b44">charAt</span><span style="color:#666">(</span>index<span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
index<span style="color:#666">+</span><span style="color:#666">+</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">if</span> <span style="color:#666">(</span>index <span style="color:#666">&gt;</span><span style="color:#666">=</span> characters<span style="color:#666">.</span><span style="color:#b44">length</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span> <span style="color:#666">{</span>
index <span style="color:#666">=</span> 0<span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
session<span style="color:#666">.</span><span style="color:#b44">setAttribute</span><span style="color:#666">(</span>INDEX_KEY<span style="color:#666">,</span> index<span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">return</span> buffer<span style="color:#666">.</span><span style="color:#b44">toString</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
<span style="color:#666">}</span>
</code></pre></div><p><img src="/assets/img/mina/codec-filter.jpeg" alt=""></p>
<h2 id="conclusion">Conclusion</h2>
<p>There is a lot more to tell about encoding and decoding. But I hope this tutorial already gets you started.
I will try to add something about the DemuxingProtocolCodecFactory in the near future.
And then we will also have a look at how to use a delimiter instead of a length prefix.</p>
<div class="nav">
<div class="nav_prev">
<a href="../ch8-iobuffer/ch8-iobuffer.html">Chapter 8 - IoBuffer</a>
</div>
<div class="nav_up">
<a href="../user-guide-toc.html">User Guide</a>
</div>
<div class="nav_next">
<a href="../ch10-executor-filter/ch10-executor-filter.html">Chapter 10 - Executor 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>