<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ashish's Tech Blog &#187; IoBuffer</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/tag/iobuffer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using Auto expanding and shrinking IoBuffer in Apache MINA</title>
		<link>http://www.ashishpaliwal.com/blog/2008/12/using-auto-expanding-and-shrinking-iobuffer-in-apache-mina/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/12/using-auto-expanding-and-shrinking-iobuffer-in-apache-mina/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 13:37:32 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[IoBuffer]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=224</guid>
		<description><![CDATA[Networking applications always need ByteBuffer that can expand on the need basis. Java ByteBuffer doesn’t provide this feature. Fortunately, MINA’s IoBuffer class has implemented this much needed feature. Creating Auto Expanding Buffer IoBuffer class in MINA has a property autoExpand, which when set makes the IoBuffer expand on the need basis, very much similar to [...]]]></description>
			<content:encoded><![CDATA[<p>Networking applications always need ByteBuffer that can expand on the need basis. Java ByteBuffer doesn’t provide this feature. Fortunately, MINA’s IoBuffer class has implemented this much needed feature.</p>
<p><strong>Creating Auto Expanding Buffer</strong></p>
<p>IoBuffer class in MINA has a property autoExpand, which when set makes the IoBuffer expand on the need basis, very much similar to StringBuffer class.<br />
Let’s see, how we can achieve this</p>
<pre class="java5">&nbsp;
IoBuffer buffer = IoBuffer.<span style="color: #006600;">allocate</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
buffer.<span style="color: #006600;">setAutoExpand</span><span style="color: #66cc66;">&#40;</span><span style="color: #b13366;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
buffer.<span style="color: #006600;">putString</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;12345678&quot;</span>, encoder<span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Add more to this buffer</span>
buffer.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">byte</span><span style="color: #66cc66;">&#41;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>When we set this property, the buffer will expand as and when needed. Under the hood, it essentially allocated a new IoBuffer and returns the new instance with the copied data.</p>
<p>So, can we shrink the buffer as well. Yes, if we set the autoshrink property to true.</p>
<pre class="java5">&nbsp;
IoBuffer buffer = IoBuffer.<span style="color: #006600;">allocate</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
buffer.<span style="color: #006600;">setAutoShrink</span><span style="color: #66cc66;">&#40;</span><span style="color: #b13366;">true</span><span style="color: #66cc66;">&#41;</span>;
buffer.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">byte</span><span style="color: #66cc66;">&#41;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Buffer size = &quot;</span>+buffer.<span style="color: #006600;">capacity</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
buffer.<span style="color: #006600;">shrink</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
buffer.<span style="color: #006600;">capacity</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Buffer size = &quot;</span>+buffer.<span style="color: #006600;">capacity</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
buffer.<span style="color: #006600;">shrink</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Buffer size = &quot;</span>+buffer.<span style="color: #006600;">capacity</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Here we set autoShrink property to true. To manually shrink the capacity, we use shrink() method. Caution, shrink(), won’t reduce capacity below the minimum capacity (the capacity with which the buffer was created). The second call to shrink(), will reduce the capacity to 8.</p>
<p>Has updated the MINA’s documentation page on IoBuffer today. So please feel free to find more details <a href="http://mina.apache.org/iobuffer.html">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/12/using-auto-expanding-and-shrinking-iobuffer-in-apache-mina/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
