mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Maarten Bosteels" <mbosteels....@gmail.com>
Subject Re: sending/receiving a byte[]
Date Fri, 07 Dec 2007 09:15:14 GMT
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 <plum@mailinator.com> 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.
>
>

Mime
View raw message