View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.codec.statemachine;
21  
22  import org.apache.mina.core.buffer.IoBuffer;
23  import org.apache.mina.filter.codec.ProtocolDecoderException;
24  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
25  
26  /**
27   * {@link DecodingState} which decodes a single <code>CRLF</code>.
28   * If it is found, the bytes are consumed and <code>true</code>
29   * is provided as the product. Otherwise, read bytes are pushed back
30   * to the stream, and <code>false</code> is provided as the
31   * product.
32   * Note that if we find a CR but do not find a following LF, we raise
33   * an error.
34   *
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36   */
37  public abstract class CrLfDecodingState implements DecodingState {
38      /**
39       * Carriage return character
40       */
41      private static final byte CR = 13;
42  
43      /**
44       * Line feed character
45       */
46      private static final byte LF = 10;
47  
48      private boolean hasCR;
49  
50      /**
51       * {@inheritDoc}
52       */
53      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
54          boolean found = false;
55          boolean finished = false;
56          while (in.hasRemaining()) {
57              byte b = in.get();
58              if (!hasCR) {
59                  if (b == CR) {
60                      hasCR = true;
61                  } else {
62                      if (b == LF) {
63                          found = true;
64                      } else {
65                          in.position(in.position() - 1);
66                          found = false;
67                      }
68                      finished = true;
69                      break;
70                  }
71              } else {
72                  if (b == LF) {
73                      found = true;
74                      finished = true;
75                      break;
76                  }
77  
78                  throw new ProtocolDecoderException("Expected LF after CR but was: " + (b & 0xff));
79              }
80          }
81  
82          if (finished) {
83              hasCR = false;
84              return finishDecode(found, out);
85          }
86  
87          return this;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
94          return finishDecode(false, out);
95      }
96  
97      /**
98       * Invoked when this state has found a <code>CRLF</code>.
99       * 
100      * @param foundCRLF <code>true</code> 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 }