while (true) { try { if (state == SEEKING_HEADER) {  messageStartPosition = indexOf(in, in.position(), HEADER_PATTERN); log.debug("found header");  position = messageStartPosition + HEADER_PATTERN.length; state = PARSING_LENGTH; } if (state == PARSING_LENGTH) { if (position >= in.limit()) { break; } byte ch = in.get(position++); while (Character.isDigit((char) ch)) { bodyLength = bodyLength * 10 + (ch - '0'); if (position < in.limit()) { ch = in.get(position++); } else { break; } } if (ch == '\001') { bodyStartPosition = position; state = READING_BODY; log.debug("reading body, length = " + bodyLength); } else if (position < in.limit()) { handleError(in, "Error in message length", false); } } if (state == READING_BODY) { if ((in.limit() - position) < bodyLength) { break; } position += bodyLength; state = PARSING_CHECKSUM; } if (state == PARSING_CHECKSUM) { if (startsWith(in, position, CHECKSUM_PATTERN)) { log.debug("parsing checksum"); position += CHECKSUM_PATTERN.length; } else { if (position + CHECKSUM_PATTERN.length < in.limit()) { handleError(in, "did not find checksum field, bad length?", isLogon(in, messageStartPosition)); if (in.remaining() > 0) { continue; } else { break; } } else { break; } } messageString = getMessageString(in); out.write(messageString); resetState(); return MessageDecoderResult.OK; } } catch (Throwable t) { state = SEEKING_HEADER; if (t instanceof ProtocolViolationException) { throw (ProtocolViolationException) t; } else { throw new ProtocolViolationException(t); }
int messageCount = 0; while (parseMessage(in, out)) { messageCount++; } if (messageCount > 0) {  if (in.remaining() < HEADER_PATTERN.length) { position = 0;
    public MessageDecoderResult decode(ProtocolSession session, ByteBuffer in,            ProtocolDecoderOutput out) throws ProtocolViolationException {        while (true) {            try {                if (state == SEEKING_HEADER) {                                        messageStartPosition = indexOf(in, in.position(), HEADER_PATTERN);                    log.debug("found header");                    position = messageStartPosition + HEADER_PATTERN.length;                    state = PARSING_LENGTH;                }                if (state == PARSING_LENGTH) {                    if (position >= in.limit()) {                        break;                    }                    byte ch = in.get(position++);                    while (Character.isDigit((char) ch)) {                        bodyLength = bodyLength * 10 + (ch - '0');                        if (position < in.limit()) {                            ch = in.get(position++);                        } else {                            break;                        }                    }                    if (ch == '\001') {                        bodyStartPosition = position;                        state = READING_BODY;                        log.debug("reading body, length = " + bodyLength);                    } else if (position < in.limit()) {                        handleError(in, "Error in message length", false);                    }                }                if (state == READING_BODY) {                    if ((in.limit() - position) < bodyLength) {                        break;                    }                    position += bodyLength;                    state = PARSING_CHECKSUM;                }                if (state == PARSING_CHECKSUM) {                    if (startsWith(in, position, CHECKSUM_PATTERN)) {                        log.debug("parsing checksum");                        position += CHECKSUM_PATTERN.length;                    } else {                        if (position + CHECKSUM_PATTERN.length < in.limit()) {                            handleError(in, "did not find checksum field, bad length?", isLogon(in,                                    messageStartPosition));                            if (in.remaining() > 0) {                                continue;                            } else {                                break;                            }                        } else {                            break;                        }                    }                    messageString = getMessageString(in);                    out.write(messageString);                    resetState();                    return MessageDecoderResult.OK;                }            } catch (Throwable t) {                state = SEEKING_HEADER;                if (t instanceof ProtocolViolationException) {                    throw (ProtocolViolationException) t;                } else {                    throw new ProtocolViolationException(t);                }            }        }        return MessageDecoderResult.NEED_DATA;    }