Hello,
I am using nonpooled heap buffers in my server.
I was wondering if I could hang on to, and re-use the ByteBuffer passed into
decode...
For example, when the decoder decodes the message I would like to set a
field in the message that points back to the original ByteBuffer. Later on,
when the server is broadcasting the same message to mutliple clients it
would just send that byte buffer instead of re-encoding the message or
sending an object.
Here is what I would like to do:
public MessageDecoderResult decode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
byte messageID = in.get();
Msg m = Msg.createMsg(messageID);
m.read(in);
// cache the byte buffer version of this message
m.setBuffer(in.flip().duplicate());
// pass message to the output
out.write(m);
return MessageDecoderResult.OK;
}
Then the app logic can do what it needs with the decoded message, and when
it comes time to forwarding the message it would just use the cached
buffered version of the message instead of pushing it through the encoder
again. I know MINA won't send ByteBuffers through the encoder. Would this
work, or should I make my own ByteBuffer and copy the message into it? Just
trying to save a little on allocations...
Thanks!
--brigham