Forwarded to the mailing list.

Please do not send direct mails.

-------- Original Message --------
Subject:        Re: sorry, I'm not able to convert byte into myClass
Date:   Mon, 08 Feb 2010 06:55:09 -0800
From:   silviasignor...@libero.it
To:     elecha...@apache.org



thanks Emmanuel Lécharny,
but my problem is not resolt...
I must send and receive std BasicMessage without the dimension.
I insert the code in.position() but I receive the message many times too!
Have you got another solution?

this is my new code
protected boolean doDecode(IoSession session, IoBuffer in, 
ProtocolDecoderOutput out) throws Exception {
                in.position();
                byte[] byteVal = new byte[in.capacity()];
                if (in.hasRemaining()){
                byteVal = in.array();
                        BasicMessage m = createBasicMessage(byteVal);
                        System.out.println("RICEVUTO DA " + m.getSender() + 
"\n");
                out.write(m);
                in.get(); //without this the host throws th exception 
doDecode() can't return true when buffer is not consumed.
                return true;
        } else {
            return false;
        }

elecharny-2 wrote:

 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


Quoted from:
http://old.nabble.com/sorry%2C-I%27m-not-able-to-convert-byte-into-myClass-tp27488607p27488650.html



Reply via email to