You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Robin Batra <ro...@gmail.com> on 2007/04/27 22:38:02 UTC

ByteBuffer converts Multiple Strings to one String

Hi !!!

I have a little problem.
I have written my own MessageEncoder for encoding my messages

For that in Encoder in encode function i use

public void encode(IoSession session, Object message, ProtocolEncoderOutput
out)
   throws Exception
 {
  Message msg = (Message) message;
  ByteBuffer buf = ByteBuffer.allocate(20);
  buf.setAutoExpand(true);


  //Add data to ByteBuffer
  buf.putString(msg.getFromID(), charsetEncoder); // FromID is String and
Encoder is UTF-8
  buf.putString(msg.getToID(), charsetEncoder ); // ToID is String and
Encoder is UTF-8
  buf.putString(msg.getTimeStamp(), charsetEncoder ); // TimeStamp is String
and Encoder is UTF-8
  buf.putInt(msg.getLength());
  buf.put(msg.getData());

  buf.flip();
  out.write(buf);

 }



Whenever i use my decoder to decode the same message it gives me all the
strings in the first call itself.

That is when i call

buf.getString(charsetDecoder); it gives me all FromID + ToID + TimeStamp and
other calls to getString returns empty strings.

What should i do and how to rectify this problem.

Thanks and Regards,

Robin

Re: ByteBuffer converts Multiple Strings to one String

Posted by Maarten Bosteels <mb...@gmail.com>.
Hello Robin,

On 4/27/07, Robin Batra <ro...@gmail.com> wrote:
>
> Hi !!!
>
> I have a little problem.
> I have written my own MessageEncoder for encoding my messages
>
> For that in Encoder in encode function i use
>
> public void encode(IoSession session, Object message,
> ProtocolEncoderOutput
> out)
>    throws Exception
> {
>   Message msg = (Message) message;
>   ByteBuffer buf = ByteBuffer.allocate(20);
>   buf.setAutoExpand(true);
>
>
>   //Add data to ByteBuffer
>   buf.putString(msg.getFromID(), charsetEncoder); // FromID is String and
> Encoder is UTF-8
>   buf.putString(msg.getToID(), charsetEncoder ); // ToID is String and
> Encoder is UTF-8
>   buf.putString(msg.getTimeStamp(), charsetEncoder ); // TimeStamp is
> String
> and Encoder is UTF-8
>   buf.putInt(msg.getLength());
>   buf.put(msg.getData());
>
>   buf.flip();
>   out.write(buf);
>
> }
>
>
>
> Whenever i use my decoder to decode the same message it gives me all the
> strings in the first call itself.
>
> That is when i call
>
> buf.getString(charsetDecoder); it gives me all FromID + ToID + TimeStamp
> and
> other calls to getString returns empty strings.
>
> What should i do


rtfm: read the fine manual :

http://mina.apache.org/report/1.1/apidocs/org/apache/mina/common/ByteBuffer.html#getString(java.nio.charset.CharsetDecoder)
http://mina.apache.org/report/1.1/apidocs/org/apache/mina/common/ByteBuffer.html#putString(java.lang.CharSequence,%20java.nio.charset.CharsetEncoder)

"Writes the content of in into this buffer using the specified encoder. This
method doesn't terminate string with NUL. You have to do it by yourself."

and how to rectify this problem.


try this:

buf.putString(msg.getFromID(), charsetEncoder);
buf.put((byte)0x00);
buf.putString(msg.getToID(), charsetEncoder );
buf.put((byte)0x00);
buf.putString(msg.getTimeStamp(), charsetEncoder );
buf.put((byte)0x00);
buf.putInt(msg.getLength());
buf.put(msg.getData());

Maarten


Thanks and Regards,
>
> Robin
>