Hello all,
Right now we have the TextLineDecoder, which always works with a delimiter.
Wouldn't it be a good idea to add a PrefixedStringDecoder ?
Something like this:
public class PrefixedStringDecoder extends CumulativeProtocolDecoder {
...
public PrefixedStringDecoder(int prefixLength, int maxDataLength,
Charset charset) {
this.prefixLength = prefixLength;
this.maxDataLength = maxDataLength;
this.charset = charset;
}
protected boolean doDecode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
if (in.prefixedDataAvailable(prefixLength, maxDataLength)) {
String msg = in.getPrefixedString(prefixLength, charset.newDecoder());
out.write(msg);
return true;
} else {
return false;
}
}
Maarten
On Dec 4, 2007 8:51 AM, Niklas Therning <[EMAIL PROTECTED]> wrote:
> guttikonda wrote:
> > Hi
> >
> > I am new to Mina.I have implemented a mina server to accept connections
> > from tcp/ip clients.The client only accepts network byte order messages with
> > message length.I figured that i can achieve it by using DataInputStream and
> > DataOutputStream readUTF() and writeUTF() methods respectively.
> >
> > When receiving data from client i am successful in getting the correct one:
> > ByteBuffer rb = (ByteBuffer)msg;
> > reader = new DataInputStream(rb.asInputStream());
> > String inputMsg = reader.readUTF();
> >
> > I want to achieve the same thing when sending the response.I have no idea
> > how i should wrap the DataOutputStream in my ByteBuffer to write it back to
> > the session.If i simply write my response string back it is not producing
> > the message length.Can anyone please help?
> >
> >
> >
> Maybe something like
>
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> DataOutputStream writer = new DataOutputStream(baos);
> writer.writeUTF(msg);
> session.write(ByteBuffer.wrap(baos.toByteArray());
>
> would work?
>
> BUT, you couldn't you simply use ByteBuffer.getPrefixedString(...) and
> ByteBuffer.putPrefixedString(...) instead?
>
> AND, you should be aware that the network layer may decide to deliver
> your message in several packets. The result is that you cannot assume
> that a ByteBuffer passed in to your IoHandler.messageReceived() method
> contains a complete message. You should look into implementing a custom
> ProtocolDecoder. There's a tutorial on this. The beauty of using
> decoder/encoder is that you will only have to deal with Strings in your
> IoHandler.
>
> HTH
> Niklas
>
>