mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert.j.gr...@jpmorgan.com
Subject Re: ByteBuffer remaining and hasRemaining
Date Wed, 14 Jun 2006 21:49:08 GMT

<br><font size=2 face="sans-serif">The idea is that in a protocol you know
what a frame looks like. For example you might know that a frame consists
of an integer id, a string command name and then depending on the particular
command, a variable number of integers.</font>
<br>
<br><font size=2 face="sans-serif">In that case you know that if you have
only 4 bytes in the buffer that you cannot fully decode a frame so you
&quot;need data&quot;. If you have enough data to read the integer plus
the string you could then look up how many integers you need following
that and if you don't have enough bytes, return that you &quot;need data&quot;.</font>
<br>
<br><font size=2 face="sans-serif">In the decode method returning null
does not mean you need data.</font>
<br>
<br><font size=2 face="sans-serif">Note that this implies that for efficiency
you should avoid designing your protocol so that you need to run through
lots of data before you can determine whether you have enough data to decode
an entire frame. So putting a size field right at the start is a good idea.
In that case, the decodable() method can be pretty simple.</font>
<br>
<br><font size=2 face="sans-serif">Robert</font>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td>
<td><font size=1 face="sans-serif"><b>&quot;Andres Quijano&quot;
&lt;tulsidas@gmail.com&gt;</b></font>
<p><font size=1 face="sans-serif">14/06/2006 21:00</font>
<br><font size=1 face="sans-serif">Please respond to mina-dev</font>
<td><font size=1 face="Arial">&nbsp; &nbsp; &nbsp; &nbsp; </font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp;
To:
&nbsp; &nbsp; &nbsp; &nbsp;mina-dev@directory.apache.org</font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp;
cc:
&nbsp; &nbsp; &nbsp; &nbsp;</font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp;
Subject:
&nbsp; &nbsp; &nbsp; &nbsp;ByteBuffer remaining and hasRemaining</font></table>
<br>
<br>
<br><font size=2><tt>Hi<br>
<br>
I'm new to MINA. I've been going through the examples and I'm not<br>
quite understanding how the NEED_DATA and returning null in the<br>
decoders work. Specifically, I do not understand the remaining and<br>
hasRemaining method in ByteBuffer.<br>
<br>
The example decoder goes like this:<br>
<br>
if (in.remaining() &lt; Constants.HEADER_LEN) {<br>
 &nbsp; return MessageDecoderResult.NEED_DATA;<br>
}<br>
<br>
I interpret it like &quot;if there are less remaining bytes than the size<br>
of the header, we need more data&quot;, which doesn't make sense.<br>
<br>
Also, I tried to do something like this in a decoder:<br>
<br>
if (in.hasRemaining()) {<br>
 &nbsp; return null; // Need more data.<br>
}<br>
<br>
On the beggining of the method. The method get's called three times,<br>
and it never returns false on the hasRemaining method.<br>
<br>
The question goes simply to: how to know if there's enough data when<br>
decoding an object with many primitives and some strings (of variable<br>
size)?<br>
<br>
Thanks<br>
</tt></font>
<br>
<br>
<br>
This communication is for informational purposes only. It is not intended<br>
as an offer or solicitation for the purchase or sale of any financial<br>
instrument or as an official confirmation of any transaction. All market prices,<br>
data and other information are not warranted as to completeness or accuracy and<br>
are subject to change without notice. Any comments or statements made herein <br>
do not necessarily reflect those of JPMorgan Chase &amp; Co., its subsidiaries <br>
and affiliates.

Mime
View raw message