Hi,

in the first case, you can set the position in the byteBuffer using the
position()  method.

However, you will have the exact same problem than in case 2 : you will get
a BufferUnderflow for the same reason  :

a call to in.hasRemaining() does not tell you that you hace enough bytes to
be able to decode a new BasicMessage. It just tells you that you have
*some*  bytes available.

>From the top of my head, I think you can call in.hasRemaining( expectedSize
)

On Sun, Feb 7, 2010 at 11:48 AM, silviasignor...@libero.it <
silviasignor...@libero.it> wrote:

> Dears,
> I've the same problem...
> I have a mina peer that communicate with myClass called BasicMessage.
>
> My problem is on decoder filter in fuction doDecode(), I convert the
> message
> into byte and receive the corret byte, but I'm not able to convert it into
> BasicMessage.
> I try two solution:
>
> <FIRST SOLUTION> My problem is that the program print line ++++++++++++++++
> many times, I
> belive that host receive the message many times because array() will not
> "move"
> forward in the IoByteBuffer. Is there a fuction that cancel the
> IoByteBuffer?
>
> public class MessageDecoder extends CumulativeProtocolDecoder {
>
>        protected boolean doDecode(IoSession session, IoBuffer in,
> ProtocolDecoderOutput out) throws Exception {
>                byte[] byteVal = null;
>    if (in.hasRemaining()){
>    byteVal = in.array();
>                        BasicMessage m = createBasicMessage(byteVal);
>                        System.out.println("RICEVUTO DA " + m.getSender() +
> "\n");++++++++++++++++
>    out.write(m);
>    in.get();
>    return true;
>    } else {
>            return false;
>        }
>    }
>
>    public static BasicMessage createBasicMessage(byte[] bytes){
>    Object object = null;
>    try{
>    object = new java.io.ObjectInputStream(new java.io.ByteArrayInputStream
> (bytes)).readObject();
>    }catch(java.io.IOException ioe){
>    java.util.logging.Logger.global.log(java.util.logging.Level.SEVERE, ioe.
> getMessage());
>    }catch(java.lang.ClassNotFoundException cnfe){
>    java.util.logging.Logger.global.log(java.util.logging.Level.SEVERE,
> cnfe.
> getMessage());
>    }
>    return (BasicMessage)object;
>    }
> }
>
>
>
>
> <SECOND SOLUTION> In this case the receiver throws the exception
> org.apache.
> mina.filter.codec.ProtocolDecoderException:
> java.nio.BufferUnderflowException
> (Hexdump: AC ... 72).
>
> protected boolean doDecode(IoSession session, IoBuffer in,
> ProtocolDecoderOutput out) throws Exception {
>    byte[] byteVal = new byte[in.capacity()];
>                if (in.hasRemaining()){
>                        in.get(byteVal , 0, in.capacity());
>                        BasicMessage m = createBasicMessage(byteVal);
>                        System.out.println("RECEIVED FROM " + m.getSender()
> +
> "\n");
>    out.write(m);
>    return true;
>    } else {
>            return false;
>        }
>
>
> Thanks
> Sia
>



-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

Reply via email to