001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.codec.statemachine;
021
022import org.apache.mina.core.buffer.IoBuffer;
023import org.apache.mina.filter.codec.ProtocolDecoderException;
024import org.apache.mina.filter.codec.ProtocolDecoderOutput;
025
026/**
027 * {@link DecodingState} which decodes a single <code>CRLF</code>.
028 * If it is found, the bytes are consumed and <tt>true</tt>
029 * is provided as the product. Otherwise, read bytes are pushed back
030 * to the stream, and <tt>false</tt> is provided as the
031 * product.
032 * Note that if we find a CR but do not find a following LF, we raise
033 * an error.
034 *
035 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
036 */
037public abstract class CrLfDecodingState implements DecodingState {
038    /**
039     * Carriage return character
040     */
041    private static final byte CR = 13;
042
043    /**
044     * Line feed character
045     */
046    private static final byte LF = 10;
047
048    private boolean hasCR;
049
050    /**
051     * {@inheritDoc}
052     */
053    public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
054        boolean found = false;
055        boolean finished = false;
056        while (in.hasRemaining()) {
057            byte b = in.get();
058            if (!hasCR) {
059                if (b == CR) {
060                    hasCR = true;
061                } else {
062                    if (b == LF) {
063                        found = true;
064                    } else {
065                        in.position(in.position() - 1);
066                        found = false;
067                    }
068                    finished = true;
069                    break;
070                }
071            } else {
072                if (b == LF) {
073                    found = true;
074                    finished = true;
075                    break;
076                }
077
078                throw new ProtocolDecoderException("Expected LF after CR but was: " + (b & 0xff));
079            }
080        }
081
082        if (finished) {
083            hasCR = false;
084            return finishDecode(found, out);
085        }
086
087        return this;
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
094        return finishDecode(false, out);
095    }
096
097    /**
098     * Invoked when this state has found a <code>CRLF</code>.
099     * 
100     * @param foundCRLF <tt>true</tt> if <code>CRLF</code> was found.
101     * @param out the current {@link ProtocolDecoderOutput} used to write 
102     *        decoded messages.
103     * @return the next state if a state transition was triggered (use 
104     *         <code>this</code> for loop transitions) or <code>null</code> if 
105     *         the state machine has reached its end.
106     * @throws Exception if the read data violated protocol specification.
107     */
108    protected abstract DecodingState finishDecode(boolean foundCRLF, ProtocolDecoderOutput out) throws Exception;
109}