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 The Apache MINA Project (dev@mina.apache.org)
36   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
37   */
38  public abstract class CrLfDecodingState implements DecodingState {
39      /**
40       * Carriage return character
41       */
42      private static final byte CR = 13;
43      
44      /**
45       * Line feed character
46       */
47      private static final byte LF = 10;
48  
49      private boolean hasCR;
50  
51      /**
52       * {@inheritDoc}
53       */
54      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
55              throws Exception {
56          boolean found = false;
57          boolean finished = false;
58          while (in.hasRemaining()) {
59              byte b = in.get();
60              if (!hasCR) {
61                  if (b == CR) {
62                      hasCR = true;
63                  } else {
64                      if (b == LF) {
65                          found = true;
66                      } else {
67                          in.position(in.position() - 1);
68                          found = false;
69                      }
70                      finished = true;
71                      break;
72                  }
73              } else {
74                  if (b == LF) {
75                      found = true;
76                      finished = true;
77                      break;
78                  } else {
79                      throw new ProtocolDecoderException(
80                              "Expected LF after CR but was: " + (b & 0xff));
81                  }
82              }
83          }
84  
85          if (finished) {
86              hasCR = false;
87              return finishDecode(found, out);
88          } else {
89              return this;
90          }
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      public DecodingState finishDecode(ProtocolDecoderOutput out)
97              throws Exception {
98          return finishDecode(false, out);
99      }
100 
101     /**
102      * Invoked when this state has found a <code>CRLF</code>.
103      * 
104      * @param foundCRLF <code>true</code> if <code>CRLF</code> was found.
105      * @param out the current {@link ProtocolDecoderOutput} used to write 
106      *        decoded messages.
107      * @return the next state if a state transition was triggered (use 
108      *         <code>this</code> for loop transitions) or <code>null</code> if 
109      *         the state machine has reached its end.
110      * @throws Exception if the read data violated protocol specification.
111      */
112     protected abstract DecodingState finishDecode(boolean foundCRLF,
113             ProtocolDecoderOutput out) throws Exception;
114 }