Cinar Eren wrote:
>
> Hi,
>
>
> I have a strange situation(imho) with ByteBuffer of MINA. What i try to
> do is, check the buffer until "\r\n" is available; and then get the
> string representation of this much of bytes:
> byte b = 0;
> int counter = 3;
> boolean hasR = true;
> String mes = null;
>
> while (in.hasRemaining()) {
> b = in.get();
> counter++;
> if (b == '\r')
> hasR = true;
> if ((hasR) && (b == '\n')) {
> break;
> }
> }
> in.flip();
> System.out.println("after 1. flip:"+in.remaining());
>
> in.remaining() returns the total data that is read until the flip. but i
> know that there are more bytes in the Buffer that are not read. I guess
> this is a normal behavior for MINA but how can make those bytes after
> \r\n available after the flip() command is called?
> Maybe i miss a very simple point???
>
> thanks in advance.
> Best regards..
Well, this isn't strange at all since flip() sets the buffer's position
to 0 and its limit to the current position. When you use get() you only
see bytes between the position and the limit. To continue reading the
bytes after the \r\n you will have to reset the position and limit to
what they were just before the flip(). Something like:
int pos = in.position();
in.flip();
... Convert into String or whatever
in.clear();
in.position(pos);
There could be better ways to achieve this.
MINA's ByteBuffer is mostly an extension of the java.nio.ByteBuffer
class. For more info on how buffers work have a look at the JavaDocs for
java.nio.Buffer and java.nio.ByteBuffer.
--
Niklas Therning
Software Architect
www.spamdrain.net