Did you mean like this:
public class requestEncoder implements ProtocolEncoder {
@Override
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
byte b;
IoBuffer buffer = IoBuffer.allocate(220, false);
buffer.setAutoExpand(true);
if (message instanceof Long )
{ b = '@';
long controllerCurrentLoad= (Long) message;
buffer.put (b);
buffer.putLong(controllerCurrentLoad);
}
if ( message instanceof Object )
{
b = '&';
controllerInfo request = (controllerInfo ) message;
buffer.put (b);
buffer.putObject(request.getControllerName());
buffer.putObject(request.getBackupControllers());
buffer.putLong(request.getControllerCapacity());
}
buffer.flip();
out.write(buffer);
}
the decoder will be:
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
byte b = 0 ;
String cName = null;
HashMap <String, Long> backupControllers = new HashMap <String,
Long>() ;
long controllerCapacity=0;
long controllerCurrentLoad = 0;
while (in.hasRemaining())
{
b = in.get();
if (b == '&')
{
cName =(String) in.getObject();
@SuppressWarnings("unchecked")
HashMap<String, Long> bc= (HashMap<String, Long>) in.getObject();
backupControllers = bc;
controllerCapacity = in.getLong();
}
else if (b == '@')
{
controllerCurrentLoad = in.getLong();
}
}
controllerInfo request = new controllerInfo (cName, backupControllers,
controllerCapacity);
request.setControllerLoad(controllerCurrentLoad);
out.write(request);
return false;
}
I tested it and it read the object, and it did not read the long.