public void decode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
put(in);
Message m = readingMessage;
try {
for (;;) {
readBuf.flip();
if (m == null) {
int limit = readBuf.limit();
boolean failed = true;
try {
m = recognizer.recognize(readBuf);
failed = false;
} finally {
if (failed) {
// clear the read buffer if failed to recognize
readBuf.clear();
break;
} else {
if (m == null) {
readBuf.limit(readBuf.capacity());
readBuf.position(limit);
break; // finish decoding
} else {
// reset buffer for read
readBuf.limit(limit);
readBuf.position(0);
}
}
}
}
if (m != null) {
try {
if (m.read(readBuf)) {
out.write(m);
m = null;
} else {
break;
}