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)
54              throws Exception {
55          boolean found = false;
56          boolean finished = false;
57          while (in.hasRemaining()) {
58              byte b = in.get();
59              if (!hasCR) {
60                  if (b == CR) {
61                      hasCR = true;
62                  } else {
63                      if (b == LF) {
64                          found = true;
65                      } else {
66                          in.position(in.position() - 1);
67                          found = false;
68                      }
69                      finished = true;
70                      break;
71                  }
72              } else {
73                  if (b == LF) {
74                      found = true;
75                      finished = true;
76                      break;
77                  }
78                  
79                  throw new ProtocolDecoderException(
80                          "Expected LF after CR but was: " + (b & 0xff));
81              }
82          }
83  
84          if (finished) {
85              hasCR = false;
86              return finishDecode(found, out);
87          }
88          
89          return this;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public DecodingState finishDecode(ProtocolDecoderOutput out)
96              throws Exception {
97          return finishDecode(false, out);
98      }
99  
100     /**
101      * Invoked when this state has found a <code>CRLF</code>.
102      * 
103      * @param foundCRLF <code>true</code> if <code>CRLF</code> was found.
104      * @param out the current {@link ProtocolDecoderOutput} used to write 
105      *        decoded messages.
106      * @return the next state if a state transition was triggered (use 
107      *         <code>this</code> for loop transitions) or <code>null</code> if 
108      *         the state machine has reached its end.
109      * @throws Exception if the read data violated protocol specification.
110      */
111     protected abstract DecodingState finishDecode(boolean foundCRLF,
112             ProtocolDecoderOutput out) throws Exception;
113 }