oops, sorry for that :-)

On Dec 7, 2007 9:48 PM, Khanh Nguyen <[EMAIL PROTECTED]> wrote:
>
> Never mind, I found the bug, the buffer in encode() must be allocate as
>
> ByteBuffer buffer = ByteBuffer.allocate( bytes.length + 4, false );
>
> instead of
>
> ByteBuffer buffer = ByteBuffer.allocate( bytes.length, false );
>
>
>
>
>
>
> Khanh Nguyen wrote:
> >
> > Hi Maarten,
> >
> > This is very helpful. I tried, but still haven't quite figured it out
> >
> > I used your code to create ByteArrayEncoder and ByteArrayDecoder verbatim.
> > Then write a simple ByteArrayCodecFactory
> >
> > public class ByteArrayCodecFactory implements ProtocolCodecFactory {
> >     private ProtocolEncoder encoder;
> >     private ProtocolDecoder decoder;
> >       public ByteArrayCodecFactory(){
> >               encoder = new ByteArrayEncoder();
> >               decoder = new ByteArrayDecoder();
> >       }
> >       public ProtocolEncoder getEncoder() throws Exception{
> >               return encoder;
> >       }
> >       public ProtocolDecoder getDecoder() throws Exception{
> >               return decoder;
> >       }
> > }
> >
> >
> > when a session created, I send a byte[] from the server to the client
> >
> > public void sessionCreated(IoSession session) throws Exception {
> >       if( session.getTransportType() == TransportType.SOCKET ){
> >               ((SocketSessionConfig) session.getConfig() 
> > ).setReceiveBufferSize( 2048
> > );
> >               ((SocketSessionConfig) session.getConfig() 
> > ).setSendBufferSize(2048);
> >       }
> >       byte[] arr = new byte[300000];
> >       for (int i = 0;i<arr.length;i++) arr[i] = 1;
> >       session.write(arr);
> > }
> >
> > , nothing happens on the client side. Also, when I look more closely, the
> > encoder seems to stop at "buffer.put(bytes);" (I put a
> > System.out.println() after and nothing was printed).
> >
> > If I want my byte[] to have a length of 300.000, should I set
> > in.prefixedDataAvailable(4,300000)?
> >
> > Thank you very much,
> >
> > Sincerely,
> >
> > Khanh Nguyen
> >
> >
> > Maarten Bosteels wrote:
> >>
> >> Hi Aquafina,
> >>
> >> You want to send and receive byte arrays, right ?
> >> If the byte array can contain any byte value, you can't use a
> >> delimiter, so the only option I see is to use a length prefix.
> >>
> >> Have a look at the codec tutorial
> >> http://mina.apache.org/tutorial-on-protocolcodecfilter.html
> >>
> >> A Codec for encoding/decoding byte[] objects is really easy.
> >> The decoder would look like this:
> >>
> >> protected boolean doDecode(IoSession session, ByteBuffer in,
> >> ProtocolDecoderOutput out) throws Exception {
> >>         if (in.prefixedDataAvailable(4)) {
> >>             int length = in.getInt();
> >>             byte[] bytes = new byte[length];
> >>             in.get(bytes);
> >>             out.write(bytes);
> >>             return true;
> >>         } else {
> >>           return false;
> >>         }
> >>     }
> >>
> >> The encoder looks like this:
> >>
> >>   public void encode(IoSession session, Object message,
> >> ProtocolEncoderOutput out) throws Exception {
> >>      byte[] bytes = (byte[]) message;
> >>     ByteBuffer buffer = ByteBuffer.allocate( bytes.length, false );
> >>     buffer.putInt(bytes.length);
> >>     buffer.put(bytes);
> >>     buffer.flip();
> >>     out.write(buffer);
> >>   }
> >>
> >> We are using this approach to send/receive xml documents. We can't use
> >> strings because we don't know which encoding
> >> (UTF-8, UTF-16, ...) the client is going to use. We pass the byte[] to
> >> the xml parser, which will correctly detect the encoding specified in
> >> the xml itself.
> >>
> >> HTH
> >> Maarten
> >>
> >>
> >> On Dec 7, 2007 5:23 AM, aquafina <[EMAIL PROTECTED]> wrote:
> >>>
> >>> Hello,
> >>>
> >>> Do I need to set ReceiveBufferSize = 300.000 if I want to send a byte
> >>> array
> >>> of 300.000 elements?
> >>>
> >>>
> >>>
> >>> Qi wrote:
> >>> >
> >>> > Hi Aquafina,
> >>> >
> >>> > I'm not a MINA expert, but as far as I know, you can wrap a byte array
> >>> > into ByteBuffer by using ByteBuffer.wrap(byte[] byteArray, int offset,
> >>> int
> >>> > length), and send it through IoSession.write(the ByteBuffer instance);
> >>> > Please be aware that by doing this, you bypass your costumized
> >>> > ProtocalEncoder/Decoder.
> >>> >
> >>> > Wrap a byte array into a serilizable object, just in sake of sending
> >>> it
> >>> > alone, is a bit redundant IMHO. The wrap object will need to go
> >>> through
> >>> > ObjectSerializationDecoder and ObjectSerializationEncoder, which add
> >>> some
> >>> > extra cost.
> >>> >
> >>> > Cheers,
> >>> > Qi
> >>> >
> >>> >
> >>> > aquafina wrote:
> >>> >>
> >>> >> Hello,
> >>> >>
> >>> >> I am new with MINA. Would anyone please show me how to configure
> >>> >> SocketAcceptorConfig to receive/send a byte[]. I tried to wrap it
> >>> with a
> >>> >> Serializable object and used
> >>> >>
> >>> >> cfg.getFilterChain().addLast(
> >>> >>                 "codec",
> >>> >>                 new ProtocolCodecFilter(
> >>> >>                         new ObjectSerializationCodecFactory()));
> >>> >>
> >>> >> but it doesn't seem to work.
> >>> >>
> >>> >> Thanks,
> >>> >>
> >>> >>
> >>> >>
> >>> >
> >>> >
> >>>
> >>> --
> >>> View this message in context:
> >>> http://www.nabble.com/sending-receiving-a-byte---tf4958040s16868.html#a14206617
> >>>
> >>> Sent from the Apache MINA Support Forum mailing list archive at
> >>> Nabble.com.
> >>>
> >>>
> >>
> >>
> >
> >
>
> --
> View this message in context: 
> http://www.nabble.com/sending-receiving-a-byte---tf4958040s16868.html#a14220485
>
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.
>
>

Reply via email to