You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Brenno Hayden <br...@gmail.com> on 2008/02/19 15:06:58 UTC

ByteBuffer

Hi,

I am having a difficulty in mke this cast? Can I do something? Some tip ?

*code*

ByteArrayDecoder extends CumulativeProtocolDecoder {

protected boolean doDecode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
     if (in instanceof ByteBuffer) {
      int len = in.limit();
      byte[] stream = new byte[len];
      //TODO
      stream = in.array(); // Why ?
      out.write(stream);
      return true;
     }else {
       return false.
     }
}

*console*:

org.apache.mina.filter.codec.ProtocolDecoderException:
java.lang.UnsupportedOperationException



Thanks


-- 
________________________
Brenno Hayden F. Dantas

Re: ByteBuffer

Posted by Christian Migowski <ch...@gmail.com>.
On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
>
>
> > I had to deal with a protocol that hasn't neither a fixed length nor a
> > fixed
> > delimiter (it is XML fragments sent directly on the wire, without any
> > header, so the Msg was complete when i found a matching end tag to a
> list
> > of
> > start tags).
>
>
> Is it a custom protocol ? I think the protocol smells a bit.
> Is the character encoding used in the XML fragments always the same ?


Of course, using a protocol that has a fixed/prefixed/clearly delimited
length is more easy - but if you have to communicate with systems that use
proprietary protocols which are not "network friendly", you have to deal
with it. For me it had an advantage: i finally got enlightened about what
SAX parsers are good for ;)

christian!

Re: ByteBuffer

Posted by Luis Neves <lu...@co.sapo.pt>.
Maarten Bosteels wrote:
> On Feb 19, 2008 4:27 PM, Luis Neves <lu...@co.sapo.pt> wrote:
> 
>> Maarten Bosteels wrote:
>>> On Feb 19, 2008 3:39 PM, Christian Migowski <ch...@gmail.com>
>> wrote:
>>
>>
>>>> I had to deal with a protocol that hasn't neither a fixed length nor a
>>>> fixed
>>>> delimiter (it is XML fragments sent directly on the wire, without any
>>>> header, so the Msg was complete when i found a matching end tag to a
>> list
>>>> of
>>>> start tags).
>>>
>>> Is it a custom protocol ? I think the protocol smells a bit.
>>> Is the character encoding used in the XML fragments always the same ?
>>>
>>> You're gonna have a hard time decoding when clients are allowed to send
>> XML
>>> using the encoding of their choice (eg. both UTF-8 and UTF-16)
>>
>> See XMMP, it is basically an endless stream of XML stanzas.
>>
>>
> Hello Luis,
> 
> I guess you mean XMPP ?

Yes... clumsy fingers.

--
Luis Neves

Re: ByteBuffer

Posted by Maarten Bosteels <mb...@gmail.com>.
On Feb 19, 2008 4:27 PM, Luis Neves <lu...@co.sapo.pt> wrote:

> Maarten Bosteels wrote:
> > On Feb 19, 2008 3:39 PM, Christian Migowski <ch...@gmail.com>
> wrote:
>
>
> >> I had to deal with a protocol that hasn't neither a fixed length nor a
> >> fixed
> >> delimiter (it is XML fragments sent directly on the wire, without any
> >> header, so the Msg was complete when i found a matching end tag to a
> list
> >> of
> >> start tags).
> >
> >
> > Is it a custom protocol ? I think the protocol smells a bit.
> > Is the character encoding used in the XML fragments always the same ?
> >
> > You're gonna have a hard time decoding when clients are allowed to send
> XML
> > using the encoding of their choice (eg. both UTF-8 and UTF-16)
>
>
> See XMMP, it is basically an endless stream of XML stanzas.
>
>
Hello Luis,

I guess you mean XMPP ?  According to [1] it uses UTF-8 character encoding
exclusively,
which makes decoding a lot easier. That's why I asked Brenno if the encoding
is fixed.

[1] http://www.xmpp.org/rfcs/rfc3920.html

regards,
Maarten

>
> --
> Luis Neves
>

Re: ByteBuffer

Posted by Luis Neves <lu...@co.sapo.pt>.
Maarten Bosteels wrote:
> On Feb 19, 2008 3:39 PM, Christian Migowski <ch...@gmail.com> wrote:


>> I had to deal with a protocol that hasn't neither a fixed length nor a
>> fixed
>> delimiter (it is XML fragments sent directly on the wire, without any
>> header, so the Msg was complete when i found a matching end tag to a list
>> of
>> start tags).
> 
> 
> Is it a custom protocol ? I think the protocol smells a bit.
> Is the character encoding used in the XML fragments always the same ?
> 
> You're gonna have a hard time decoding when clients are allowed to send XML
> using the encoding of their choice (eg. both UTF-8 and UTF-16)


See XMMP, it is basically an endless stream of XML stanzas.


--
Luis Neves

Re: ByteBuffer

Posted by Maarten Bosteels <mb...@gmail.com>.
On Feb 19, 2008 3:39 PM, Christian Migowski <ch...@gmail.com> wrote:

> I am curious, how would that ByteArrayCodecFactory help?


It would just be convenient for people that need to send/receive
a byte[] with a length prefix.

>
>
> I had to deal with a protocol that hasn't neither a fixed length nor a
> fixed
> delimiter (it is XML fragments sent directly on the wire, without any
> header, so the Msg was complete when i found a matching end tag to a list
> of
> start tags).


Is it a custom protocol ? I think the protocol smells a bit.
Is the character encoding used in the XML fragments always the same ?

You're gonna have a hard time decoding when clients are allowed to send XML
using the encoding of their choice (eg. both UTF-8 and UTF-16)

Maarten


> The reason may be that i am still inexperienced with the Java
> way of doing things, but the solution i found was to read up to a certain
> amount of bytes (using the method below), check if the message was
> complete,
> if not rewind the buffer to the start position and wait for the next
> invocation.
>
> christian!
>
>
> On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
> >
> > Have a look at
> >
> >
> http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
> >
> > Is your array of bytes prefixed with a length indicator ?
> > If not, how do you know the end of the message ?
> >
> > Maybe we should add a ByteArrayCodecFactory to MINA.
> >
> > Maarten
> >
> > On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com>
> wrote:
> >
> > > Hi,
> > >
> > > i stumbled over this as well some time ago. The Javadoc says this
> > > exception
> > > happens when the ByteBuffer isn't backed by an accessible array, my
> > > finding
> > > was it isn't guaranteed in any way it is - better do not rely on that
> > > method.
> > > Fortunately you can easily rewrite your code to
> > >
> > >     int len = in.remaining();
> > > //(i do think your usage of limit() is not what you intend to do)
> > >     byte[] stream = new byte[len];
> > >     in.get(stream , 0, in.remaining());
> > >
> > >
> > > hope that helps.
> > > regards,
> > > christian!
> > >
> > >
> > > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > > >
> > > > Hi,
> > > >
> > > > I am having a difficulty in mke this cast? Can I do something? Some
> > tip
> > > ?
> > > >
> > > > *code*
> > > >
> > > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > > >
> > > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > > ProtocolDecoderOutput out) throws Exception {
> > > >     if (in instanceof ByteBuffer) {
> > > >      int len = in.limit();
> > > >      byte[] stream = new byte[len];
> > > >      //TODO
> > > >      stream = in.array(); // Why ?
> > > >      out.write(stream);
> > > >      return true;
> > > >     }else {
> > > >       return false.
> > > >     }
> > > > }
> > > >
> > > > *console*:
> > > >
> > > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > > java.lang.UnsupportedOperationException
> > > >
> > > >
> > > >
> > > > Thanks
> > > >
> > > >
> > > > --
> > > > ________________________
> > > > Brenno Hayden F. Dantas
> > > >
> > >
> >
>

Re: ByteBuffer

Posted by Brenno Hayden <br...@gmail.com>.
http://svn.apache.org/viewvc/mina/tags/2.0.0-M1/

On Feb 19, 2008 1:51 PM, Brenno Hayden <br...@gmail.com> wrote:

> what's link svn 2.0 ?i'm not remember
> thanks
>
> On Feb 19, 2008 12:47 PM, Brenno Hayden <br...@gmail.com> wrote:
>
> > Maarten,
> >  i will consider MINA 2.0!
> >
> > thanks!
> >
> >
> >
> > On Feb 19, 2008 12:03 PM, Maarten Bosteels <mb...@gmail.com>
> > wrote:
> >
> > > Hello Brenno,
> > >
> > > Some comments on the code you showed:
> > > * when you're starting a new project, consider using MINA 2.0
> > > * your decoder isn't really decoding: it's not detecting the end of
> > > one
> > > message
> > >  but just copying all available bytes to a byte[]
> > >  You could just as well have no ProtocolCodecFilter in your chain
> > >  and handle the IoBuffer in your IoHandler
> > > * I have no experience with the StreamIoHandler but I don't think it's
> > > supposed
> > >  to be combined with a ProtocolCodecFilter
> > >
> > > Either your protocol uses a fixed character encoding, and then your
> > > decoder
> > > could be similar to the TextLine decoder (detecting the last closing
> > > tag
> > > instead of EOL)
> > >
> > > or your protocol must support multiple character encodings and then
> > > ... I
> > > don't know.
> > > maybe read until you can determine the used encoding ?
> > >
> > > Maarten
> > >
> > >
> > > On Feb 19, 2008 4:14 PM, Brenno Hayden <br...@gmail.com> wrote:
> > >
> > > > Christian,
> > > >  follow below my example, but there is a problem, I connect the
> > > telnet and
> > > > sending an array of bytes (5356000B000030390000) in the form of a
> > > hex,
> > > > more
> > > > in the codec does not correspond with the mesm value. Example i send
> > > > (5356000B000030390000) in hex, but in console output
> > > > *Console*:
> > > > Server is loading....
> > > > Handler.sessionOpened()
> > > > b[0] = 53
> > > > b[1] = 51
> > > > b[2] = 53
> > > > b[3] = 54
> > > > b[4] = 48
> > > > b[5] = 48
> > > > b[6] = 48
> > > > b[7] = 66
> > > > b[8] = 48
> > > > b[9] = 48
> > > > b[10] = 48
> > > > b[11] = 48
> > > > b[12] = 51
> > > > b[13] = 48
> > > > b[14] = 51
> > > > b[15] = 57
> > > > b[16] = 48
> > > > b[17] = 48
> > > > b[18] = 48
> > > > b[19] = 48
> > > > b[20] = 13
> > > > b[21] = 10
> > > > Server.sessionClosed()
> > > >
> > > >
> > > > *MyServer.Java*
> > > >
> > > > import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
> > > > import org.apache.mina.transport.socket.nio.*;
> > > > import codec.ByteArrayProtocolCodecFactory;
> > > >
> > > > public class Server {
> > > >
> > > > private static int TCP_PORT = 4443;
> > > >
> > > >    public static void main(String[] args) {
> > > >
> > > >        IoAcceptor acceptor = new SocketAcceptor();
> > > >        IoAcceptorConfig config = new SocketAcceptorConfig();
> > > >        DefaultIoFilterChainBuilder chain = config.getFilterChain();
> > > >        // Bind
> > > >        chain.addLast("mycode", new ProtocolCodecFilter(
> > > >                new ByteArrayProtocolCodecFactory()));
> > > >        try {
> > > >            acceptor.bind(new InetSocketAddress(TCP_PORT), new
> > > MyHandler(),
> > > >                    config);
> > > >        } catch (IOException e) {
> > > >            // TODO Auto-generated catch block
> > > >            e.printStackTrace();
> > > >        }
> > > >        System.out.println("Server is loading....");
> > > >    }
> > > > //-----------------------
> > > > *MyHandler.java*
> > > >
> > > > import java.io.*;
> > > >
> > > > import org.apache.mina.common.*;
> > > > import org.apache.mina.handler.StreamIoHandler;
> > > >
> > > > public class Handler extends StreamIoHandler {
> > > >
> > > >    public void sessionOpened(IoSession session) {
> > > >        System.out.println("MyHandler.sessionOpened()");
> > > >    }
> > > >    public void messageReceived(IoSession session, Object message) {
> > > >        System.out.println("MyHandler.messageReceived()");
> > > >        byte[] b = (byte[])message;
> > > >        for(int i=0; i<b.length; i++) {
> > > >            System.out.println("b["+i+"] = "+b[i]);
> > > >        }
> > > >    }
> > > > public void messageSent(IoSession session, Object message)  {
> > > >        System.out.println("MyHandler.messageSent");
> > > >    }
> > > >
> > > >    public void sessionClosed(IoSession session) throws Exception {
> > > >        System.out.println("MyHandler.sessionClosed()");
> > > >        session.close();
> > > >    }
> > > >
> > > >    public void sessionIdle(IoSession session, IdleStatus status) {
> > > >        System.out.println("MyServer.sessionIdle()");
> > > >        session.close();
> > > >    }
> > > >
> > > >
> > > >    protected void processStreamIo(IoSession session, InputStream
> > > arg1,
> > > >            OutputStream arg2) {
> > > >        System.out.println("MyServer.processStreamIo()");
> > > >
> > > >
> > > >    }
> > > > //----------
> > > > *ByteArrayProtocolCodecFactory.java*
> > > >
> > > > package codec;
> > > >
> > > > import org.apache.mina.filter.codec.ProtocolCodecFactory;
> > > > import org.apache.mina.filter.codec.ProtocolDecoder;
> > > > import org.apache.mina.filter.codec.ProtocolEncoder;
> > > >
> > > > public class ByteArrayProtocolCodecFactory implements
> > > ProtocolCodecFactory
> > > > {
> > > >    private ProtocolEncoder encoder;
> > > >    private ProtocolDecoder decoder;
> > > >
> > > >    public ByteArrayProtocolCodecFactory() {
> > > >        encoder = new ByteArrayProtocolEnconder();
> > > >        decoder = new ByteArrayProtocolDecoder();
> > > >    }
> > > >
> > > >    public ProtocolEncoder getEncoder() throws Exception {
> > > >        return encoder;
> > > >    }
> > > >
> > > >    public ProtocolDecoder getDecoder() throws Exception {
> > > >        return decoder;
> > > >    }
> > > > }
> > > > //---
> > > > *ByteArrayProtocolDecoder.java*
> > > >
> > > > package codec;
> > > >
> > > > import org.apache.mina.common.ByteBuffer;
> > > > import org.apache.mina.common.IoSession;
> > > > import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
> > > > import org.apache.mina.filter.codec.ProtocolDecoderOutput;
> > > >
> > > > public class ByteArrayProtocolDecoder extends
> > > CumulativeProtocolDecoder {
> > > >
> > > >    public ByteArrayProtocolDecoder() {
> > > >    }
> > > >
> > > >    @Override
> > > >    protected boolean doDecode(IoSession session, ByteBuffer in,
> > > >            ProtocolDecoderOutput output) throws Exception {
> > > >        int len = in.remaining();
> > > >        byte[] data = new byte[len];
> > > >        in.get(data);
> > > >        output.write(data);
> > > >        return true;
> > > >    }
> > > > }
> > > > //----------
> > > > *ByteArrayProtocolEnconder.java*
> > > >
> > > > package codec;
> > > >
> > > > import org.apache.mina.common.ByteBuffer;
> > > > import org.apache.mina.common.IoSession;
> > > > import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
> > > > import org.apache.mina.filter.codec.ProtocolEncoderOutput;
> > > >
> > > > public class ByteArrayProtocolEnconder extends
> > > ProtocolEncoderAdapter {
> > > >
> > > >    public ByteArrayProtocolEnconder() {
> > > >
> > > >    }
> > > >
> > > >    public void encode(IoSession session, Object message,
> > > > ProtocolEncoderOutput output)
> > > >            throws Exception {
> > > >        byte[] bytes = (byte[]) message;
> > > >        int totalLength = bytes.length;
> > > >        ByteBuffer buf = ByteBuffer.allocate(totalLength);
> > > >        buf.put(bytes);
> > > >        buf.flip();
> > > >        output.write(buf);
> > > >    }
> > > > }
> > > >
> > > > On Feb 19, 2008 10:39 AM, Christian Migowski <ch...@gmail.com>
> > > wrote:
> > > >
> > > > > I am curious, how would that ByteArrayCodecFactory help?
> > > > >
> > > > > I had to deal with a protocol that hasn't neither a fixed length
> > > nor a
> > > > > fixed
> > > > > delimiter (it is XML fragments sent directly on the wire, without
> > > any
> > > > > header, so the Msg was complete when i found a matching end tag to
> > > a
> > > > list
> > > > > of
> > > > > start tags). The reason may be that i am still inexperienced with
> > > the
> > > > Java
> > > > > way of doing things, but the solution i found was to read up to a
> > > > certain
> > > > > amount of bytes (using the method below), check if the message was
> > > > > complete,
> > > > > if not rewind the buffer to the start position and wait for the
> > > next
> > > > > invocation.
> > > > >
> > > > > christian!
> > > > >
> > > > >
> > > > > On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
> > > > > >
> > > > > > Have a look at
> > > > > >
> > > > > >
> > > > >
> > > >
> > > http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
> > > > > >
> > > > > > Is your array of bytes prefixed with a length indicator ?
> > > > > > If not, how do you know the end of the message ?
> > > > > >
> > > > > > Maybe we should add a ByteArrayCodecFactory to MINA.
> > > > > >
> > > > > > Maarten
> > > > > >
> > > > > > On Feb 19, 2008 3:19 PM, Christian Migowski <
> > > chrismfwrd@gmail.com>
> > > > > wrote:
> > > > > >
> > > > > > > Hi,
> > > > > > >
> > > > > > > i stumbled over this as well some time ago. The Javadoc says
> > > this
> > > > > > > exception
> > > > > > > happens when the ByteBuffer isn't backed by an accessible
> > > array, my
> > > > > > > finding
> > > > > > > was it isn't guaranteed in any way it is - better do not rely
> > > on
> > > > that
> > > > > > > method.
> > > > > > > Fortunately you can easily rewrite your code to
> > > > > > >
> > > > > > >     int len = in.remaining();
> > > > > > > //(i do think your usage of limit() is not what you intend to
> > > do)
> > > > > > >     byte[] stream = new byte[len];
> > > > > > >     in.get(stream , 0, in.remaining());
> > > > > > >
> > > > > > >
> > > > > > > hope that helps.
> > > > > > > regards,
> > > > > > > christian!
> > > > > > >
> > > > > > >
> > > > > > > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > > > > > > >
> > > > > > > > Hi,
> > > > > > > >
> > > > > > > > I am having a difficulty in mke this cast? Can I do
> > > something?
> > > > Some
> > > > > > tip
> > > > > > > ?
> > > > > > > >
> > > > > > > > *code*
> > > > > > > >
> > > > > > > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > > > > > > >
> > > > > > > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > > > > > > ProtocolDecoderOutput out) throws Exception {
> > > > > > > >     if (in instanceof ByteBuffer) {
> > > > > > > >      int len = in.limit();
> > > > > > > >      byte[] stream = new byte[len];
> > > > > > > >      //TODO
> > > > > > > >      stream = in.array(); // Why ?
> > > > > > > >      out.write(stream);
> > > > > > > >      return true;
> > > > > > > >     }else {
> > > > > > > >       return false.
> > > > > > > >     }
> > > > > > > > }
> > > > > > > >
> > > > > > > > *console*:
> > > > > > > >
> > > > > > > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > > > > > > java.lang.UnsupportedOperationException
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks
> > > > > > > >
> > > > > > > >
> > > > > > > > --
> > > > > > > > ________________________
> > > > > > > > Brenno Hayden F. Dantas
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > ________________________
> > > > Brenno Hayden F. Dantas
> > > >
> > >
> >
> >
> >
> > --
> > ________________________
> > Brenno Hayden F. Dantas
>
>
>
>
> --
> ________________________
> Brenno Hayden F. Dantas




-- 
________________________
Brenno Hayden F. Dantas

Re: ByteBuffer

Posted by Brenno Hayden <br...@gmail.com>.
what's link svn 2.0 ?i'm not remember
thanks
On Feb 19, 2008 12:47 PM, Brenno Hayden <br...@gmail.com> wrote:

> Maarten,
>  i will consider MINA 2.0!
>
> thanks!
>
>
>
> On Feb 19, 2008 12:03 PM, Maarten Bosteels <mb...@gmail.com>
> wrote:
>
> > Hello Brenno,
> >
> > Some comments on the code you showed:
> > * when you're starting a new project, consider using MINA 2.0
> > * your decoder isn't really decoding: it's not detecting the end of one
> > message
> >  but just copying all available bytes to a byte[]
> >  You could just as well have no ProtocolCodecFilter in your chain
> >  and handle the IoBuffer in your IoHandler
> > * I have no experience with the StreamIoHandler but I don't think it's
> > supposed
> >  to be combined with a ProtocolCodecFilter
> >
> > Either your protocol uses a fixed character encoding, and then your
> > decoder
> > could be similar to the TextLine decoder (detecting the last closing tag
> > instead of EOL)
> >
> > or your protocol must support multiple character encodings and then ...
> > I
> > don't know.
> > maybe read until you can determine the used encoding ?
> >
> > Maarten
> >
> >
> > On Feb 19, 2008 4:14 PM, Brenno Hayden <br...@gmail.com> wrote:
> >
> > > Christian,
> > >  follow below my example, but there is a problem, I connect the telnet
> > and
> > > sending an array of bytes (5356000B000030390000) in the form of a hex,
> > > more
> > > in the codec does not correspond with the mesm value. Example i send
> > > (5356000B000030390000) in hex, but in console output
> > > *Console*:
> > > Server is loading....
> > > Handler.sessionOpened()
> > > b[0] = 53
> > > b[1] = 51
> > > b[2] = 53
> > > b[3] = 54
> > > b[4] = 48
> > > b[5] = 48
> > > b[6] = 48
> > > b[7] = 66
> > > b[8] = 48
> > > b[9] = 48
> > > b[10] = 48
> > > b[11] = 48
> > > b[12] = 51
> > > b[13] = 48
> > > b[14] = 51
> > > b[15] = 57
> > > b[16] = 48
> > > b[17] = 48
> > > b[18] = 48
> > > b[19] = 48
> > > b[20] = 13
> > > b[21] = 10
> > > Server.sessionClosed()
> > >
> > >
> > > *MyServer.Java*
> > >
> > > import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
> > > import org.apache.mina.transport.socket.nio.*;
> > > import codec.ByteArrayProtocolCodecFactory;
> > >
> > > public class Server {
> > >
> > > private static int TCP_PORT = 4443;
> > >
> > >    public static void main(String[] args) {
> > >
> > >        IoAcceptor acceptor = new SocketAcceptor();
> > >        IoAcceptorConfig config = new SocketAcceptorConfig();
> > >        DefaultIoFilterChainBuilder chain = config.getFilterChain();
> > >        // Bind
> > >        chain.addLast("mycode", new ProtocolCodecFilter(
> > >                new ByteArrayProtocolCodecFactory()));
> > >        try {
> > >            acceptor.bind(new InetSocketAddress(TCP_PORT), new
> > MyHandler(),
> > >                    config);
> > >        } catch (IOException e) {
> > >            // TODO Auto-generated catch block
> > >            e.printStackTrace();
> > >        }
> > >        System.out.println("Server is loading....");
> > >    }
> > > //-----------------------
> > > *MyHandler.java*
> > >
> > > import java.io.*;
> > >
> > > import org.apache.mina.common.*;
> > > import org.apache.mina.handler.StreamIoHandler;
> > >
> > > public class Handler extends StreamIoHandler {
> > >
> > >    public void sessionOpened(IoSession session) {
> > >        System.out.println("MyHandler.sessionOpened()");
> > >    }
> > >    public void messageReceived(IoSession session, Object message) {
> > >        System.out.println("MyHandler.messageReceived()");
> > >        byte[] b = (byte[])message;
> > >        for(int i=0; i<b.length; i++) {
> > >            System.out.println("b["+i+"] = "+b[i]);
> > >        }
> > >    }
> > > public void messageSent(IoSession session, Object message)  {
> > >        System.out.println("MyHandler.messageSent");
> > >    }
> > >
> > >    public void sessionClosed(IoSession session) throws Exception {
> > >        System.out.println("MyHandler.sessionClosed()");
> > >        session.close();
> > >    }
> > >
> > >    public void sessionIdle(IoSession session, IdleStatus status) {
> > >        System.out.println("MyServer.sessionIdle()");
> > >        session.close();
> > >    }
> > >
> > >
> > >    protected void processStreamIo(IoSession session, InputStream arg1,
> > >            OutputStream arg2) {
> > >        System.out.println("MyServer.processStreamIo()");
> > >
> > >
> > >    }
> > > //----------
> > > *ByteArrayProtocolCodecFactory.java*
> > >
> > > package codec;
> > >
> > > import org.apache.mina.filter.codec.ProtocolCodecFactory;
> > > import org.apache.mina.filter.codec.ProtocolDecoder;
> > > import org.apache.mina.filter.codec.ProtocolEncoder;
> > >
> > > public class ByteArrayProtocolCodecFactory implements
> > ProtocolCodecFactory
> > > {
> > >    private ProtocolEncoder encoder;
> > >    private ProtocolDecoder decoder;
> > >
> > >    public ByteArrayProtocolCodecFactory() {
> > >        encoder = new ByteArrayProtocolEnconder();
> > >        decoder = new ByteArrayProtocolDecoder();
> > >    }
> > >
> > >    public ProtocolEncoder getEncoder() throws Exception {
> > >        return encoder;
> > >    }
> > >
> > >    public ProtocolDecoder getDecoder() throws Exception {
> > >        return decoder;
> > >    }
> > > }
> > > //---
> > > *ByteArrayProtocolDecoder.java*
> > >
> > > package codec;
> > >
> > > import org.apache.mina.common.ByteBuffer;
> > > import org.apache.mina.common.IoSession;
> > > import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
> > > import org.apache.mina.filter.codec.ProtocolDecoderOutput;
> > >
> > > public class ByteArrayProtocolDecoder extends
> > CumulativeProtocolDecoder {
> > >
> > >    public ByteArrayProtocolDecoder() {
> > >    }
> > >
> > >    @Override
> > >    protected boolean doDecode(IoSession session, ByteBuffer in,
> > >            ProtocolDecoderOutput output) throws Exception {
> > >        int len = in.remaining();
> > >        byte[] data = new byte[len];
> > >        in.get(data);
> > >        output.write(data);
> > >        return true;
> > >    }
> > > }
> > > //----------
> > > *ByteArrayProtocolEnconder.java*
> > >
> > > package codec;
> > >
> > > import org.apache.mina.common.ByteBuffer;
> > > import org.apache.mina.common.IoSession;
> > > import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
> > > import org.apache.mina.filter.codec.ProtocolEncoderOutput;
> > >
> > > public class ByteArrayProtocolEnconder extends ProtocolEncoderAdapter
> > {
> > >
> > >    public ByteArrayProtocolEnconder() {
> > >
> > >    }
> > >
> > >    public void encode(IoSession session, Object message,
> > > ProtocolEncoderOutput output)
> > >            throws Exception {
> > >        byte[] bytes = (byte[]) message;
> > >        int totalLength = bytes.length;
> > >        ByteBuffer buf = ByteBuffer.allocate(totalLength);
> > >        buf.put(bytes);
> > >        buf.flip();
> > >        output.write(buf);
> > >    }
> > > }
> > >
> > > On Feb 19, 2008 10:39 AM, Christian Migowski <ch...@gmail.com>
> > wrote:
> > >
> > > > I am curious, how would that ByteArrayCodecFactory help?
> > > >
> > > > I had to deal with a protocol that hasn't neither a fixed length nor
> > a
> > > > fixed
> > > > delimiter (it is XML fragments sent directly on the wire, without
> > any
> > > > header, so the Msg was complete when i found a matching end tag to a
> > > list
> > > > of
> > > > start tags). The reason may be that i am still inexperienced with
> > the
> > > Java
> > > > way of doing things, but the solution i found was to read up to a
> > > certain
> > > > amount of bytes (using the method below), check if the message was
> > > > complete,
> > > > if not rewind the buffer to the start position and wait for the next
> > > > invocation.
> > > >
> > > > christian!
> > > >
> > > >
> > > > On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
> > > > >
> > > > > Have a look at
> > > > >
> > > > >
> > > >
> > >
> > http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
> > > > >
> > > > > Is your array of bytes prefixed with a length indicator ?
> > > > > If not, how do you know the end of the message ?
> > > > >
> > > > > Maybe we should add a ByteArrayCodecFactory to MINA.
> > > > >
> > > > > Maarten
> > > > >
> > > > > On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com>
> > > > wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > i stumbled over this as well some time ago. The Javadoc says
> > this
> > > > > > exception
> > > > > > happens when the ByteBuffer isn't backed by an accessible array,
> > my
> > > > > > finding
> > > > > > was it isn't guaranteed in any way it is - better do not rely on
> > > that
> > > > > > method.
> > > > > > Fortunately you can easily rewrite your code to
> > > > > >
> > > > > >     int len = in.remaining();
> > > > > > //(i do think your usage of limit() is not what you intend to
> > do)
> > > > > >     byte[] stream = new byte[len];
> > > > > >     in.get(stream , 0, in.remaining());
> > > > > >
> > > > > >
> > > > > > hope that helps.
> > > > > > regards,
> > > > > > christian!
> > > > > >
> > > > > >
> > > > > > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > > > > > >
> > > > > > > Hi,
> > > > > > >
> > > > > > > I am having a difficulty in mke this cast? Can I do something?
> > > Some
> > > > > tip
> > > > > > ?
> > > > > > >
> > > > > > > *code*
> > > > > > >
> > > > > > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > > > > > >
> > > > > > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > > > > > ProtocolDecoderOutput out) throws Exception {
> > > > > > >     if (in instanceof ByteBuffer) {
> > > > > > >      int len = in.limit();
> > > > > > >      byte[] stream = new byte[len];
> > > > > > >      //TODO
> > > > > > >      stream = in.array(); // Why ?
> > > > > > >      out.write(stream);
> > > > > > >      return true;
> > > > > > >     }else {
> > > > > > >       return false.
> > > > > > >     }
> > > > > > > }
> > > > > > >
> > > > > > > *console*:
> > > > > > >
> > > > > > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > > > > > java.lang.UnsupportedOperationException
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > Thanks
> > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > > ________________________
> > > > > > > Brenno Hayden F. Dantas
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> > >
> > >
> > > --
> > > ________________________
> > > Brenno Hayden F. Dantas
> > >
> >
>
>
>
> --
> ________________________
> Brenno Hayden F. Dantas




-- 
________________________
Brenno Hayden F. Dantas

Re: ByteBuffer

Posted by Brenno Hayden <br...@gmail.com>.
Maarten,
 i will consider MINA 2.0!

thanks!


On Feb 19, 2008 12:03 PM, Maarten Bosteels <mb...@gmail.com> wrote:

> Hello Brenno,
>
> Some comments on the code you showed:
> * when you're starting a new project, consider using MINA 2.0
> * your decoder isn't really decoding: it's not detecting the end of one
> message
>  but just copying all available bytes to a byte[]
>  You could just as well have no ProtocolCodecFilter in your chain
>  and handle the IoBuffer in your IoHandler
> * I have no experience with the StreamIoHandler but I don't think it's
> supposed
>  to be combined with a ProtocolCodecFilter
>
> Either your protocol uses a fixed character encoding, and then your
> decoder
> could be similar to the TextLine decoder (detecting the last closing tag
> instead of EOL)
>
> or your protocol must support multiple character encodings and then ... I
> don't know.
> maybe read until you can determine the used encoding ?
>
> Maarten
>
>
> On Feb 19, 2008 4:14 PM, Brenno Hayden <br...@gmail.com> wrote:
>
> > Christian,
> >  follow below my example, but there is a problem, I connect the telnet
> and
> > sending an array of bytes (5356000B000030390000) in the form of a hex,
> > more
> > in the codec does not correspond with the mesm value. Example i send
> > (5356000B000030390000) in hex, but in console output
> > *Console*:
> > Server is loading....
> > Handler.sessionOpened()
> > b[0] = 53
> > b[1] = 51
> > b[2] = 53
> > b[3] = 54
> > b[4] = 48
> > b[5] = 48
> > b[6] = 48
> > b[7] = 66
> > b[8] = 48
> > b[9] = 48
> > b[10] = 48
> > b[11] = 48
> > b[12] = 51
> > b[13] = 48
> > b[14] = 51
> > b[15] = 57
> > b[16] = 48
> > b[17] = 48
> > b[18] = 48
> > b[19] = 48
> > b[20] = 13
> > b[21] = 10
> > Server.sessionClosed()
> >
> >
> > *MyServer.Java*
> >
> > import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
> > import org.apache.mina.transport.socket.nio.*;
> > import codec.ByteArrayProtocolCodecFactory;
> >
> > public class Server {
> >
> > private static int TCP_PORT = 4443;
> >
> >    public static void main(String[] args) {
> >
> >        IoAcceptor acceptor = new SocketAcceptor();
> >        IoAcceptorConfig config = new SocketAcceptorConfig();
> >        DefaultIoFilterChainBuilder chain = config.getFilterChain();
> >        // Bind
> >        chain.addLast("mycode", new ProtocolCodecFilter(
> >                new ByteArrayProtocolCodecFactory()));
> >        try {
> >            acceptor.bind(new InetSocketAddress(TCP_PORT), new
> MyHandler(),
> >                    config);
> >        } catch (IOException e) {
> >            // TODO Auto-generated catch block
> >            e.printStackTrace();
> >        }
> >        System.out.println("Server is loading....");
> >    }
> > //-----------------------
> > *MyHandler.java*
> >
> > import java.io.*;
> >
> > import org.apache.mina.common.*;
> > import org.apache.mina.handler.StreamIoHandler;
> >
> > public class Handler extends StreamIoHandler {
> >
> >    public void sessionOpened(IoSession session) {
> >        System.out.println("MyHandler.sessionOpened()");
> >    }
> >    public void messageReceived(IoSession session, Object message) {
> >        System.out.println("MyHandler.messageReceived()");
> >        byte[] b = (byte[])message;
> >        for(int i=0; i<b.length; i++) {
> >            System.out.println("b["+i+"] = "+b[i]);
> >        }
> >    }
> > public void messageSent(IoSession session, Object message)  {
> >        System.out.println("MyHandler.messageSent");
> >    }
> >
> >    public void sessionClosed(IoSession session) throws Exception {
> >        System.out.println("MyHandler.sessionClosed()");
> >        session.close();
> >    }
> >
> >    public void sessionIdle(IoSession session, IdleStatus status) {
> >        System.out.println("MyServer.sessionIdle()");
> >        session.close();
> >    }
> >
> >
> >    protected void processStreamIo(IoSession session, InputStream arg1,
> >            OutputStream arg2) {
> >        System.out.println("MyServer.processStreamIo()");
> >
> >
> >    }
> > //----------
> > *ByteArrayProtocolCodecFactory.java*
> >
> > package codec;
> >
> > import org.apache.mina.filter.codec.ProtocolCodecFactory;
> > import org.apache.mina.filter.codec.ProtocolDecoder;
> > import org.apache.mina.filter.codec.ProtocolEncoder;
> >
> > public class ByteArrayProtocolCodecFactory implements
> ProtocolCodecFactory
> > {
> >    private ProtocolEncoder encoder;
> >    private ProtocolDecoder decoder;
> >
> >    public ByteArrayProtocolCodecFactory() {
> >        encoder = new ByteArrayProtocolEnconder();
> >        decoder = new ByteArrayProtocolDecoder();
> >    }
> >
> >    public ProtocolEncoder getEncoder() throws Exception {
> >        return encoder;
> >    }
> >
> >    public ProtocolDecoder getDecoder() throws Exception {
> >        return decoder;
> >    }
> > }
> > //---
> > *ByteArrayProtocolDecoder.java*
> >
> > package codec;
> >
> > import org.apache.mina.common.ByteBuffer;
> > import org.apache.mina.common.IoSession;
> > import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
> > import org.apache.mina.filter.codec.ProtocolDecoderOutput;
> >
> > public class ByteArrayProtocolDecoder extends CumulativeProtocolDecoder
> {
> >
> >    public ByteArrayProtocolDecoder() {
> >    }
> >
> >    @Override
> >    protected boolean doDecode(IoSession session, ByteBuffer in,
> >            ProtocolDecoderOutput output) throws Exception {
> >        int len = in.remaining();
> >        byte[] data = new byte[len];
> >        in.get(data);
> >        output.write(data);
> >        return true;
> >    }
> > }
> > //----------
> > *ByteArrayProtocolEnconder.java*
> >
> > package codec;
> >
> > import org.apache.mina.common.ByteBuffer;
> > import org.apache.mina.common.IoSession;
> > import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
> > import org.apache.mina.filter.codec.ProtocolEncoderOutput;
> >
> > public class ByteArrayProtocolEnconder extends ProtocolEncoderAdapter {
> >
> >    public ByteArrayProtocolEnconder() {
> >
> >    }
> >
> >    public void encode(IoSession session, Object message,
> > ProtocolEncoderOutput output)
> >            throws Exception {
> >        byte[] bytes = (byte[]) message;
> >        int totalLength = bytes.length;
> >        ByteBuffer buf = ByteBuffer.allocate(totalLength);
> >        buf.put(bytes);
> >        buf.flip();
> >        output.write(buf);
> >    }
> > }
> >
> > On Feb 19, 2008 10:39 AM, Christian Migowski <ch...@gmail.com>
> wrote:
> >
> > > I am curious, how would that ByteArrayCodecFactory help?
> > >
> > > I had to deal with a protocol that hasn't neither a fixed length nor a
> > > fixed
> > > delimiter (it is XML fragments sent directly on the wire, without any
> > > header, so the Msg was complete when i found a matching end tag to a
> > list
> > > of
> > > start tags). The reason may be that i am still inexperienced with the
> > Java
> > > way of doing things, but the solution i found was to read up to a
> > certain
> > > amount of bytes (using the method below), check if the message was
> > > complete,
> > > if not rewind the buffer to the start position and wait for the next
> > > invocation.
> > >
> > > christian!
> > >
> > >
> > > On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
> > > >
> > > > Have a look at
> > > >
> > > >
> > >
> >
> http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
> > > >
> > > > Is your array of bytes prefixed with a length indicator ?
> > > > If not, how do you know the end of the message ?
> > > >
> > > > Maybe we should add a ByteArrayCodecFactory to MINA.
> > > >
> > > > Maarten
> > > >
> > > > On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com>
> > > wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > i stumbled over this as well some time ago. The Javadoc says this
> > > > > exception
> > > > > happens when the ByteBuffer isn't backed by an accessible array,
> my
> > > > > finding
> > > > > was it isn't guaranteed in any way it is - better do not rely on
> > that
> > > > > method.
> > > > > Fortunately you can easily rewrite your code to
> > > > >
> > > > >     int len = in.remaining();
> > > > > //(i do think your usage of limit() is not what you intend to do)
> > > > >     byte[] stream = new byte[len];
> > > > >     in.get(stream , 0, in.remaining());
> > > > >
> > > > >
> > > > > hope that helps.
> > > > > regards,
> > > > > christian!
> > > > >
> > > > >
> > > > > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > > > > >
> > > > > > Hi,
> > > > > >
> > > > > > I am having a difficulty in mke this cast? Can I do something?
> > Some
> > > > tip
> > > > > ?
> > > > > >
> > > > > > *code*
> > > > > >
> > > > > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > > > > >
> > > > > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > > > > ProtocolDecoderOutput out) throws Exception {
> > > > > >     if (in instanceof ByteBuffer) {
> > > > > >      int len = in.limit();
> > > > > >      byte[] stream = new byte[len];
> > > > > >      //TODO
> > > > > >      stream = in.array(); // Why ?
> > > > > >      out.write(stream);
> > > > > >      return true;
> > > > > >     }else {
> > > > > >       return false.
> > > > > >     }
> > > > > > }
> > > > > >
> > > > > > *console*:
> > > > > >
> > > > > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > > > > java.lang.UnsupportedOperationException
> > > > > >
> > > > > >
> > > > > >
> > > > > > Thanks
> > > > > >
> > > > > >
> > > > > > --
> > > > > > ________________________
> > > > > > Brenno Hayden F. Dantas
> > > > > >
> > > > >
> > > >
> > >
> >
> >
> >
> > --
> > ________________________
> > Brenno Hayden F. Dantas
> >
>



-- 
________________________
Brenno Hayden F. Dantas

Re: ByteBuffer

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

Some comments on the code you showed:
* when you're starting a new project, consider using MINA 2.0
* your decoder isn't really decoding: it's not detecting the end of one
message
  but just copying all available bytes to a byte[]
  You could just as well have no ProtocolCodecFilter in your chain
  and handle the IoBuffer in your IoHandler
* I have no experience with the StreamIoHandler but I don't think it's
supposed
  to be combined with a ProtocolCodecFilter

Either your protocol uses a fixed character encoding, and then your decoder
could be similar to the TextLine decoder (detecting the last closing tag
instead of EOL)

or your protocol must support multiple character encodings and then ... I
don't know.
maybe read until you can determine the used encoding ?

Maarten


On Feb 19, 2008 4:14 PM, Brenno Hayden <br...@gmail.com> wrote:

> Christian,
>  follow below my example, but there is a problem, I connect the telnet and
> sending an array of bytes (5356000B000030390000) in the form of a hex,
> more
> in the codec does not correspond with the mesm value. Example i send
> (5356000B000030390000) in hex, but in console output
> *Console*:
> Server is loading....
> Handler.sessionOpened()
> b[0] = 53
> b[1] = 51
> b[2] = 53
> b[3] = 54
> b[4] = 48
> b[5] = 48
> b[6] = 48
> b[7] = 66
> b[8] = 48
> b[9] = 48
> b[10] = 48
> b[11] = 48
> b[12] = 51
> b[13] = 48
> b[14] = 51
> b[15] = 57
> b[16] = 48
> b[17] = 48
> b[18] = 48
> b[19] = 48
> b[20] = 13
> b[21] = 10
> Server.sessionClosed()
>
>
> *MyServer.Java*
>
> import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
> import org.apache.mina.transport.socket.nio.*;
> import codec.ByteArrayProtocolCodecFactory;
>
> public class Server {
>
> private static int TCP_PORT = 4443;
>
>    public static void main(String[] args) {
>
>        IoAcceptor acceptor = new SocketAcceptor();
>        IoAcceptorConfig config = new SocketAcceptorConfig();
>        DefaultIoFilterChainBuilder chain = config.getFilterChain();
>        // Bind
>        chain.addLast("mycode", new ProtocolCodecFilter(
>                new ByteArrayProtocolCodecFactory()));
>        try {
>            acceptor.bind(new InetSocketAddress(TCP_PORT), new MyHandler(),
>                    config);
>        } catch (IOException e) {
>            // TODO Auto-generated catch block
>            e.printStackTrace();
>        }
>        System.out.println("Server is loading....");
>    }
> //-----------------------
> *MyHandler.java*
>
> import java.io.*;
>
> import org.apache.mina.common.*;
> import org.apache.mina.handler.StreamIoHandler;
>
> public class Handler extends StreamIoHandler {
>
>    public void sessionOpened(IoSession session) {
>        System.out.println("MyHandler.sessionOpened()");
>    }
>    public void messageReceived(IoSession session, Object message) {
>        System.out.println("MyHandler.messageReceived()");
>        byte[] b = (byte[])message;
>        for(int i=0; i<b.length; i++) {
>            System.out.println("b["+i+"] = "+b[i]);
>        }
>    }
> public void messageSent(IoSession session, Object message)  {
>        System.out.println("MyHandler.messageSent");
>    }
>
>    public void sessionClosed(IoSession session) throws Exception {
>        System.out.println("MyHandler.sessionClosed()");
>        session.close();
>    }
>
>    public void sessionIdle(IoSession session, IdleStatus status) {
>        System.out.println("MyServer.sessionIdle()");
>        session.close();
>    }
>
>
>    protected void processStreamIo(IoSession session, InputStream arg1,
>            OutputStream arg2) {
>        System.out.println("MyServer.processStreamIo()");
>
>
>    }
> //----------
> *ByteArrayProtocolCodecFactory.java*
>
> package codec;
>
> import org.apache.mina.filter.codec.ProtocolCodecFactory;
> import org.apache.mina.filter.codec.ProtocolDecoder;
> import org.apache.mina.filter.codec.ProtocolEncoder;
>
> public class ByteArrayProtocolCodecFactory implements ProtocolCodecFactory
> {
>    private ProtocolEncoder encoder;
>    private ProtocolDecoder decoder;
>
>    public ByteArrayProtocolCodecFactory() {
>        encoder = new ByteArrayProtocolEnconder();
>        decoder = new ByteArrayProtocolDecoder();
>    }
>
>    public ProtocolEncoder getEncoder() throws Exception {
>        return encoder;
>    }
>
>    public ProtocolDecoder getDecoder() throws Exception {
>        return decoder;
>    }
> }
> //---
> *ByteArrayProtocolDecoder.java*
>
> package codec;
>
> import org.apache.mina.common.ByteBuffer;
> import org.apache.mina.common.IoSession;
> import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
> import org.apache.mina.filter.codec.ProtocolDecoderOutput;
>
> public class ByteArrayProtocolDecoder extends CumulativeProtocolDecoder {
>
>    public ByteArrayProtocolDecoder() {
>    }
>
>    @Override
>    protected boolean doDecode(IoSession session, ByteBuffer in,
>            ProtocolDecoderOutput output) throws Exception {
>        int len = in.remaining();
>        byte[] data = new byte[len];
>        in.get(data);
>        output.write(data);
>        return true;
>    }
> }
> //----------
> *ByteArrayProtocolEnconder.java*
>
> package codec;
>
> import org.apache.mina.common.ByteBuffer;
> import org.apache.mina.common.IoSession;
> import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
> import org.apache.mina.filter.codec.ProtocolEncoderOutput;
>
> public class ByteArrayProtocolEnconder extends ProtocolEncoderAdapter {
>
>    public ByteArrayProtocolEnconder() {
>
>    }
>
>    public void encode(IoSession session, Object message,
> ProtocolEncoderOutput output)
>            throws Exception {
>        byte[] bytes = (byte[]) message;
>        int totalLength = bytes.length;
>        ByteBuffer buf = ByteBuffer.allocate(totalLength);
>        buf.put(bytes);
>        buf.flip();
>        output.write(buf);
>    }
> }
>
> On Feb 19, 2008 10:39 AM, Christian Migowski <ch...@gmail.com> wrote:
>
> > I am curious, how would that ByteArrayCodecFactory help?
> >
> > I had to deal with a protocol that hasn't neither a fixed length nor a
> > fixed
> > delimiter (it is XML fragments sent directly on the wire, without any
> > header, so the Msg was complete when i found a matching end tag to a
> list
> > of
> > start tags). The reason may be that i am still inexperienced with the
> Java
> > way of doing things, but the solution i found was to read up to a
> certain
> > amount of bytes (using the method below), check if the message was
> > complete,
> > if not rewind the buffer to the start position and wait for the next
> > invocation.
> >
> > christian!
> >
> >
> > On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
> > >
> > > Have a look at
> > >
> > >
> >
> http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
> > >
> > > Is your array of bytes prefixed with a length indicator ?
> > > If not, how do you know the end of the message ?
> > >
> > > Maybe we should add a ByteArrayCodecFactory to MINA.
> > >
> > > Maarten
> > >
> > > On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com>
> > wrote:
> > >
> > > > Hi,
> > > >
> > > > i stumbled over this as well some time ago. The Javadoc says this
> > > > exception
> > > > happens when the ByteBuffer isn't backed by an accessible array, my
> > > > finding
> > > > was it isn't guaranteed in any way it is - better do not rely on
> that
> > > > method.
> > > > Fortunately you can easily rewrite your code to
> > > >
> > > >     int len = in.remaining();
> > > > //(i do think your usage of limit() is not what you intend to do)
> > > >     byte[] stream = new byte[len];
> > > >     in.get(stream , 0, in.remaining());
> > > >
> > > >
> > > > hope that helps.
> > > > regards,
> > > > christian!
> > > >
> > > >
> > > > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > > > >
> > > > > Hi,
> > > > >
> > > > > I am having a difficulty in mke this cast? Can I do something?
> Some
> > > tip
> > > > ?
> > > > >
> > > > > *code*
> > > > >
> > > > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > > > >
> > > > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > > > ProtocolDecoderOutput out) throws Exception {
> > > > >     if (in instanceof ByteBuffer) {
> > > > >      int len = in.limit();
> > > > >      byte[] stream = new byte[len];
> > > > >      //TODO
> > > > >      stream = in.array(); // Why ?
> > > > >      out.write(stream);
> > > > >      return true;
> > > > >     }else {
> > > > >       return false.
> > > > >     }
> > > > > }
> > > > >
> > > > > *console*:
> > > > >
> > > > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > > > java.lang.UnsupportedOperationException
> > > > >
> > > > >
> > > > >
> > > > > Thanks
> > > > >
> > > > >
> > > > > --
> > > > > ________________________
> > > > > Brenno Hayden F. Dantas
> > > > >
> > > >
> > >
> >
>
>
>
> --
> ________________________
> Brenno Hayden F. Dantas
>

Re: ByteBuffer

Posted by Christian Migowski <ch...@gmail.com>.
Hi Brenno,

i don't know how you send your data so I cannot tell for sure. Just one
point: Keep in mind that in Java "byte" is a signed type (how very useful
that is!!!), maybe this is causing you problems.

christian!



On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
>
> Christian,
> follow below my example, but there is a problem, I connect the telnet and
> sending an array of bytes (5356000B000030390000) in the form of a hex,
> more
> in the codec does not correspond with the mesm value. Example i send
> (5356000B000030390000) in hex, but in console output
> *Console*:
> Server is loading....
> Handler.sessionOpened()
> b[0] = 53
> b[1] = 51
> b[2] = 53
> b[3] = 54
> b[4] = 48
> b[5] = 48
> b[6] = 48
> b[7] = 66
> b[8] = 48
> b[9] = 48
> b[10] = 48
> b[11] = 48
> b[12] = 51
> b[13] = 48
> b[14] = 51
> b[15] = 57
> b[16] = 48
> b[17] = 48
> b[18] = 48
> b[19] = 48
> b[20] = 13
> b[21] = 10
> Server.sessionClosed()
>
>
> *MyServer.Java*
>
> import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
> import org.apache.mina.transport.socket.nio.*;
> import codec.ByteArrayProtocolCodecFactory;
>
> public class Server {
>
> private static int TCP_PORT = 4443;
>
>    public static void main(String[] args) {
>
>        IoAcceptor acceptor = new SocketAcceptor();
>        IoAcceptorConfig config = new SocketAcceptorConfig();
>        DefaultIoFilterChainBuilder chain = config.getFilterChain();
>        // Bind
>        chain.addLast("mycode", new ProtocolCodecFilter(
>                new ByteArrayProtocolCodecFactory()));
>        try {
>            acceptor.bind(new InetSocketAddress(TCP_PORT), new MyHandler(),
>                    config);
>        } catch (IOException e) {
>            // TODO Auto-generated catch block
>            e.printStackTrace();
>        }
>        System.out.println("Server is loading....");
>    }
> //-----------------------
> *MyHandler.java*
>
> import java.io.*;
>
> import org.apache.mina.common.*;
> import org.apache.mina.handler.StreamIoHandler;
>
> public class Handler extends StreamIoHandler {
>
>    public void sessionOpened(IoSession session) {
>        System.out.println("MyHandler.sessionOpened()");
>    }
>    public void messageReceived(IoSession session, Object message) {
>        System.out.println("MyHandler.messageReceived()");
>        byte[] b = (byte[])message;
>        for(int i=0; i<b.length; i++) {
>            System.out.println("b["+i+"] = "+b[i]);
>        }
>    }
> public void messageSent(IoSession session, Object message)  {
>        System.out.println("MyHandler.messageSent");
>    }
>
>    public void sessionClosed(IoSession session) throws Exception {
>        System.out.println("MyHandler.sessionClosed()");
>        session.close();
>    }
>
>    public void sessionIdle(IoSession session, IdleStatus status) {
>        System.out.println("MyServer.sessionIdle()");
>        session.close();
>    }
>
>
>    protected void processStreamIo(IoSession session, InputStream arg1,
>            OutputStream arg2) {
>        System.out.println("MyServer.processStreamIo()");
>
>
>    }
> //----------
> *ByteArrayProtocolCodecFactory.java*
>
> package codec;
>
> import org.apache.mina.filter.codec.ProtocolCodecFactory;
> import org.apache.mina.filter.codec.ProtocolDecoder;
> import org.apache.mina.filter.codec.ProtocolEncoder;
>
> public class ByteArrayProtocolCodecFactory implements ProtocolCodecFactory
> {
>    private ProtocolEncoder encoder;
>    private ProtocolDecoder decoder;
>
>    public ByteArrayProtocolCodecFactory() {
>        encoder = new ByteArrayProtocolEnconder();
>        decoder = new ByteArrayProtocolDecoder();
>    }
>
>    public ProtocolEncoder getEncoder() throws Exception {
>        return encoder;
>    }
>
>    public ProtocolDecoder getDecoder() throws Exception {
>        return decoder;
>    }
> }
> //---
> *ByteArrayProtocolDecoder.java*
>
> package codec;
>
> import org.apache.mina.common.ByteBuffer;
> import org.apache.mina.common.IoSession;
> import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
> import org.apache.mina.filter.codec.ProtocolDecoderOutput;
>
> public class ByteArrayProtocolDecoder extends CumulativeProtocolDecoder {
>
>    public ByteArrayProtocolDecoder() {
>    }
>
>    @Override
>    protected boolean doDecode(IoSession session, ByteBuffer in,
>            ProtocolDecoderOutput output) throws Exception {
>        int len = in.remaining();
>        byte[] data = new byte[len];
>        in.get(data);
>        output.write(data);
>        return true;
>    }
> }
> //----------
> *ByteArrayProtocolEnconder.java*
>
> package codec;
>
> import org.apache.mina.common.ByteBuffer;
> import org.apache.mina.common.IoSession;
> import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
> import org.apache.mina.filter.codec.ProtocolEncoderOutput;
>
> public class ByteArrayProtocolEnconder extends ProtocolEncoderAdapter {
>
>    public ByteArrayProtocolEnconder() {
>
>    }
>
>    public void encode(IoSession session, Object message,
> ProtocolEncoderOutput output)
>            throws Exception {
>        byte[] bytes = (byte[]) message;
>        int totalLength = bytes.length;
>        ByteBuffer buf = ByteBuffer.allocate(totalLength);
>        buf.put(bytes);
>        buf.flip();
>        output.write(buf);
>    }
> }
>
> On Feb 19, 2008 10:39 AM, Christian Migowski <ch...@gmail.com> wrote:
>
> > I am curious, how would that ByteArrayCodecFactory help?
> >
> > I had to deal with a protocol that hasn't neither a fixed length nor a
> > fixed
> > delimiter (it is XML fragments sent directly on the wire, without any
> > header, so the Msg was complete when i found a matching end tag to a
> list
> > of
> > start tags). The reason may be that i am still inexperienced with the
> Java
> > way of doing things, but the solution i found was to read up to a
> certain
> > amount of bytes (using the method below), check if the message was
> > complete,
> > if not rewind the buffer to the start position and wait for the next
> > invocation.
> >
> > christian!
> >
> >
> > On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
> > >
> > > Have a look at
> > >
> > >
> >
> http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
> > >
> > > Is your array of bytes prefixed with a length indicator ?
> > > If not, how do you know the end of the message ?
> > >
> > > Maybe we should add a ByteArrayCodecFactory to MINA.
> > >
> > > Maarten
> > >
> > > On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com>
> > wrote:
> > >
> > > > Hi,
> > > >
> > > > i stumbled over this as well some time ago. The Javadoc says this
> > > > exception
> > > > happens when the ByteBuffer isn't backed by an accessible array, my
> > > > finding
> > > > was it isn't guaranteed in any way it is - better do not rely on
> that
> > > > method.
> > > > Fortunately you can easily rewrite your code to
> > > >
> > > >     int len = in.remaining();
> > > > //(i do think your usage of limit() is not what you intend to do)
> > > >     byte[] stream = new byte[len];
> > > >     in.get(stream , 0, in.remaining());
> > > >
> > > >
> > > > hope that helps.
> > > > regards,
> > > > christian!
> > > >
> > > >
> > > > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > > > >
> > > > > Hi,
> > > > >
> > > > > I am having a difficulty in mke this cast? Can I do something?
> Some
> > > tip
> > > > ?
> > > > >
> > > > > *code*
> > > > >
> > > > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > > > >
> > > > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > > > ProtocolDecoderOutput out) throws Exception {
> > > > >     if (in instanceof ByteBuffer) {
> > > > >      int len = in.limit();
> > > > >      byte[] stream = new byte[len];
> > > > >      //TODO
> > > > >      stream = in.array(); // Why ?
> > > > >      out.write(stream);
> > > > >      return true;
> > > > >     }else {
> > > > >       return false.
> > > > >     }
> > > > > }
> > > > >
> > > > > *console*:
> > > > >
> > > > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > > > java.lang.UnsupportedOperationException
> > > > >
> > > > >
> > > > >
> > > > > Thanks
> > > > >
> > > > >
> > > > > --
> > > > > ________________________
> > > > > Brenno Hayden F. Dantas
> > > > >
> > > >
> > >
> >
>
>
>
> --
> ________________________
> Brenno Hayden F. Dantas
>

Re: ByteBuffer

Posted by Brenno Hayden <br...@gmail.com>.
Christian,
 follow below my example, but there is a problem, I connect the telnet and
sending an array of bytes (5356000B000030390000) in the form of a hex, more
in the codec does not correspond with the mesm value. Example i send
(5356000B000030390000) in hex, but in console output
*Console*:
Server is loading....
Handler.sessionOpened()
b[0] = 53
b[1] = 51
b[2] = 53
b[3] = 54
b[4] = 48
b[5] = 48
b[6] = 48
b[7] = 66
b[8] = 48
b[9] = 48
b[10] = 48
b[11] = 48
b[12] = 51
b[13] = 48
b[14] = 51
b[15] = 57
b[16] = 48
b[17] = 48
b[18] = 48
b[19] = 48
b[20] = 13
b[21] = 10
Server.sessionClosed()


*MyServer.Java*

import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.*;
import codec.ByteArrayProtocolCodecFactory;

public class Server {

private static int TCP_PORT = 4443;

    public static void main(String[] args) {

        IoAcceptor acceptor = new SocketAcceptor();
        IoAcceptorConfig config = new SocketAcceptorConfig();
        DefaultIoFilterChainBuilder chain = config.getFilterChain();
        // Bind
        chain.addLast("mycode", new ProtocolCodecFilter(
                new ByteArrayProtocolCodecFactory()));
        try {
            acceptor.bind(new InetSocketAddress(TCP_PORT), new MyHandler(),
                    config);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Server is loading....");
    }
//-----------------------
*MyHandler.java*

import java.io.*;

import org.apache.mina.common.*;
import org.apache.mina.handler.StreamIoHandler;

public class Handler extends StreamIoHandler {

    public void sessionOpened(IoSession session) {
        System.out.println("MyHandler.sessionOpened()");
    }
    public void messageReceived(IoSession session, Object message) {
        System.out.println("MyHandler.messageReceived()");
        byte[] b = (byte[])message;
        for(int i=0; i<b.length; i++) {
            System.out.println("b["+i+"] = "+b[i]);
        }
    }
public void messageSent(IoSession session, Object message)  {
        System.out.println("MyHandler.messageSent");
    }

    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("MyHandler.sessionClosed()");
        session.close();
    }

    public void sessionIdle(IoSession session, IdleStatus status) {
        System.out.println("MyServer.sessionIdle()");
        session.close();
    }


    protected void processStreamIo(IoSession session, InputStream arg1,
            OutputStream arg2) {
        System.out.println("MyServer.processStreamIo()");


    }
//----------
*ByteArrayProtocolCodecFactory.java*

package codec;

import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

public class ByteArrayProtocolCodecFactory implements ProtocolCodecFactory {
    private ProtocolEncoder encoder;
    private ProtocolDecoder decoder;

    public ByteArrayProtocolCodecFactory() {
        encoder = new ByteArrayProtocolEnconder();
        decoder = new ByteArrayProtocolDecoder();
    }

    public ProtocolEncoder getEncoder() throws Exception {
        return encoder;
    }

    public ProtocolDecoder getDecoder() throws Exception {
        return decoder;
    }
}
//---
*ByteArrayProtocolDecoder.java*

package codec;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

public class ByteArrayProtocolDecoder extends CumulativeProtocolDecoder {

    public ByteArrayProtocolDecoder() {
    }

    @Override
    protected boolean doDecode(IoSession session, ByteBuffer in,
            ProtocolDecoderOutput output) throws Exception {
        int len = in.remaining();
        byte[] data = new byte[len];
        in.get(data);
        output.write(data);
        return true;
    }
}
//----------
*ByteArrayProtocolEnconder.java*

package codec;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class ByteArrayProtocolEnconder extends ProtocolEncoderAdapter {

    public ByteArrayProtocolEnconder() {

    }

    public void encode(IoSession session, Object message,
ProtocolEncoderOutput output)
            throws Exception {
        byte[] bytes = (byte[]) message;
        int totalLength = bytes.length;
        ByteBuffer buf = ByteBuffer.allocate(totalLength);
        buf.put(bytes);
        buf.flip();
        output.write(buf);
    }
}

On Feb 19, 2008 10:39 AM, Christian Migowski <ch...@gmail.com> wrote:

> I am curious, how would that ByteArrayCodecFactory help?
>
> I had to deal with a protocol that hasn't neither a fixed length nor a
> fixed
> delimiter (it is XML fragments sent directly on the wire, without any
> header, so the Msg was complete when i found a matching end tag to a list
> of
> start tags). The reason may be that i am still inexperienced with the Java
> way of doing things, but the solution i found was to read up to a certain
> amount of bytes (using the method below), check if the message was
> complete,
> if not rewind the buffer to the start position and wait for the next
> invocation.
>
> christian!
>
>
> On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
> >
> > Have a look at
> >
> >
> http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
> >
> > Is your array of bytes prefixed with a length indicator ?
> > If not, how do you know the end of the message ?
> >
> > Maybe we should add a ByteArrayCodecFactory to MINA.
> >
> > Maarten
> >
> > On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com>
> wrote:
> >
> > > Hi,
> > >
> > > i stumbled over this as well some time ago. The Javadoc says this
> > > exception
> > > happens when the ByteBuffer isn't backed by an accessible array, my
> > > finding
> > > was it isn't guaranteed in any way it is - better do not rely on that
> > > method.
> > > Fortunately you can easily rewrite your code to
> > >
> > >     int len = in.remaining();
> > > //(i do think your usage of limit() is not what you intend to do)
> > >     byte[] stream = new byte[len];
> > >     in.get(stream , 0, in.remaining());
> > >
> > >
> > > hope that helps.
> > > regards,
> > > christian!
> > >
> > >
> > > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > > >
> > > > Hi,
> > > >
> > > > I am having a difficulty in mke this cast? Can I do something? Some
> > tip
> > > ?
> > > >
> > > > *code*
> > > >
> > > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > > >
> > > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > > ProtocolDecoderOutput out) throws Exception {
> > > >     if (in instanceof ByteBuffer) {
> > > >      int len = in.limit();
> > > >      byte[] stream = new byte[len];
> > > >      //TODO
> > > >      stream = in.array(); // Why ?
> > > >      out.write(stream);
> > > >      return true;
> > > >     }else {
> > > >       return false.
> > > >     }
> > > > }
> > > >
> > > > *console*:
> > > >
> > > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > > java.lang.UnsupportedOperationException
> > > >
> > > >
> > > >
> > > > Thanks
> > > >
> > > >
> > > > --
> > > > ________________________
> > > > Brenno Hayden F. Dantas
> > > >
> > >
> >
>



-- 
________________________
Brenno Hayden F. Dantas

Re: ByteBuffer

Posted by Christian Migowski <ch...@gmail.com>.
I am curious, how would that ByteArrayCodecFactory help?

I had to deal with a protocol that hasn't neither a fixed length nor a fixed
delimiter (it is XML fragments sent directly on the wire, without any
header, so the Msg was complete when i found a matching end tag to a list of
start tags). The reason may be that i am still inexperienced with the Java
way of doing things, but the solution i found was to read up to a certain
amount of bytes (using the method below), check if the message was complete,
if not rewind the buffer to the start position and wait for the next
invocation.

christian!


On 2/19/08, Maarten Bosteels <mb...@gmail.com> wrote:
>
> Have a look at
>
> http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
>
> Is your array of bytes prefixed with a length indicator ?
> If not, how do you know the end of the message ?
>
> Maybe we should add a ByteArrayCodecFactory to MINA.
>
> Maarten
>
> On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com> wrote:
>
> > Hi,
> >
> > i stumbled over this as well some time ago. The Javadoc says this
> > exception
> > happens when the ByteBuffer isn't backed by an accessible array, my
> > finding
> > was it isn't guaranteed in any way it is - better do not rely on that
> > method.
> > Fortunately you can easily rewrite your code to
> >
> >     int len = in.remaining();
> > //(i do think your usage of limit() is not what you intend to do)
> >     byte[] stream = new byte[len];
> >     in.get(stream , 0, in.remaining());
> >
> >
> > hope that helps.
> > regards,
> > christian!
> >
> >
> > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > >
> > > Hi,
> > >
> > > I am having a difficulty in mke this cast? Can I do something? Some
> tip
> > ?
> > >
> > > *code*
> > >
> > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > >
> > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > ProtocolDecoderOutput out) throws Exception {
> > >     if (in instanceof ByteBuffer) {
> > >      int len = in.limit();
> > >      byte[] stream = new byte[len];
> > >      //TODO
> > >      stream = in.array(); // Why ?
> > >      out.write(stream);
> > >      return true;
> > >     }else {
> > >       return false.
> > >     }
> > > }
> > >
> > > *console*:
> > >
> > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > java.lang.UnsupportedOperationException
> > >
> > >
> > >
> > > Thanks
> > >
> > >
> > > --
> > > ________________________
> > > Brenno Hayden F. Dantas
> > >
> >
>

Re: ByteBuffer

Posted by Brenno Hayden <br...@gmail.com>.
Thanks  Maarten and Christian!

On Feb 19, 2008 10:23 AM, Maarten Bosteels <mb...@gmail.com> wrote:

> Have a look at
>
> http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html
>
> Is your array of bytes prefixed with a length indicator ?
> If not, how do you know the end of the message ?
>
> Maybe we should add a ByteArrayCodecFactory to MINA.
>
> Maarten
>
> On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com> wrote:
>
> > Hi,
> >
> > i stumbled over this as well some time ago. The Javadoc says this
> > exception
> > happens when the ByteBuffer isn't backed by an accessible array, my
> > finding
> > was it isn't guaranteed in any way it is - better do not rely on that
> > method.
> > Fortunately you can easily rewrite your code to
> >
> >     int len = in.remaining();
> > //(i do think your usage of limit() is not what you intend to do)
> >     byte[] stream = new byte[len];
> >     in.get(stream , 0, in.remaining());
> >
> >
> > hope that helps.
> > regards,
> > christian!
> >
> >
> > On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> > >
> > > Hi,
> > >
> > > I am having a difficulty in mke this cast? Can I do something? Some
> tip
> > ?
> > >
> > > *code*
> > >
> > > ByteArrayDecoder extends CumulativeProtocolDecoder {
> > >
> > > protected boolean doDecode(IoSession session, ByteBuffer in,
> > > ProtocolDecoderOutput out) throws Exception {
> > >     if (in instanceof ByteBuffer) {
> > >      int len = in.limit();
> > >      byte[] stream = new byte[len];
> > >      //TODO
> > >      stream = in.array(); // Why ?
> > >      out.write(stream);
> > >      return true;
> > >     }else {
> > >       return false.
> > >     }
> > > }
> > >
> > > *console*:
> > >
> > > org.apache.mina.filter.codec.ProtocolDecoderException:
> > > java.lang.UnsupportedOperationException
> > >
> > >
> > >
> > > Thanks
> > >
> > >
> > > --
> > > ________________________
> > > Brenno Hayden F. Dantas
> > >
> >
>



-- 
________________________
Brenno Hayden F. Dantas

Re: ByteBuffer

Posted by Maarten Bosteels <mb...@gmail.com>.
Have a look at
http://www.nabble.com/Re%3A-sending-receiving-a-byte---p14220485s16868.html

Is your array of bytes prefixed with a length indicator ?
If not, how do you know the end of the message ?

Maybe we should add a ByteArrayCodecFactory to MINA.

Maarten

On Feb 19, 2008 3:19 PM, Christian Migowski <ch...@gmail.com> wrote:

> Hi,
>
> i stumbled over this as well some time ago. The Javadoc says this
> exception
> happens when the ByteBuffer isn't backed by an accessible array, my
> finding
> was it isn't guaranteed in any way it is - better do not rely on that
> method.
> Fortunately you can easily rewrite your code to
>
>     int len = in.remaining();
> //(i do think your usage of limit() is not what you intend to do)
>     byte[] stream = new byte[len];
>     in.get(stream , 0, in.remaining());
>
>
> hope that helps.
> regards,
> christian!
>
>
> On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
> >
> > Hi,
> >
> > I am having a difficulty in mke this cast? Can I do something? Some tip
> ?
> >
> > *code*
> >
> > ByteArrayDecoder extends CumulativeProtocolDecoder {
> >
> > protected boolean doDecode(IoSession session, ByteBuffer in,
> > ProtocolDecoderOutput out) throws Exception {
> >     if (in instanceof ByteBuffer) {
> >      int len = in.limit();
> >      byte[] stream = new byte[len];
> >      //TODO
> >      stream = in.array(); // Why ?
> >      out.write(stream);
> >      return true;
> >     }else {
> >       return false.
> >     }
> > }
> >
> > *console*:
> >
> > org.apache.mina.filter.codec.ProtocolDecoderException:
> > java.lang.UnsupportedOperationException
> >
> >
> >
> > Thanks
> >
> >
> > --
> > ________________________
> > Brenno Hayden F. Dantas
> >
>

Re: ByteBuffer

Posted by Christian Migowski <ch...@gmail.com>.
Hi,

i stumbled over this as well some time ago. The Javadoc says this exception
happens when the ByteBuffer isn't backed by an accessible array, my finding
was it isn't guaranteed in any way it is - better do not rely on that
method.
Fortunately you can easily rewrite your code to

     int len = in.remaining();
//(i do think your usage of limit() is not what you intend to do)
     byte[] stream = new byte[len];
     in.get(stream , 0, in.remaining());


hope that helps.
regards,
christian!


On 2/19/08, Brenno Hayden <br...@gmail.com> wrote:
>
> Hi,
>
> I am having a difficulty in mke this cast? Can I do something? Some tip ?
>
> *code*
>
> ByteArrayDecoder extends CumulativeProtocolDecoder {
>
> protected boolean doDecode(IoSession session, ByteBuffer in,
> ProtocolDecoderOutput out) throws Exception {
>     if (in instanceof ByteBuffer) {
>      int len = in.limit();
>      byte[] stream = new byte[len];
>      //TODO
>      stream = in.array(); // Why ?
>      out.write(stream);
>      return true;
>     }else {
>       return false.
>     }
> }
>
> *console*:
>
> org.apache.mina.filter.codec.ProtocolDecoderException:
> java.lang.UnsupportedOperationException
>
>
>
> Thanks
>
>
> --
> ________________________
> Brenno Hayden F. Dantas
>