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.ProtocolDecoderOutput;
24  
25  /**
26   * {@link DecodingState} which consumes all bytes until a <code>CRLF</code> 
27   * has been encountered.
28   * 
29   * @author The Apache MINA Project (dev@mina.apache.org)
30   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
31   */
32  public abstract class ConsumeToCrLfDecodingState implements DecodingState {
33  
34      /**
35       * Carriage return character
36       */
37      private static final byte CR = 13;
38  
39      /**
40       * Line feed character
41       */
42      private static final byte LF = 10;
43  
44      private boolean lastIsCR;
45  
46      private IoBuffer buffer;
47  
48      /**
49       * Creates a new instance.
50       */
51      public ConsumeToCrLfDecodingState() {
52      }
53  
54      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
55              throws Exception {
56          int beginPos = in.position();
57          int limit = in.limit();
58          int terminatorPos = -1;
59  
60          for (int i = beginPos; i < limit; i++) {
61              byte b = in.get(i);
62              if (b == CR) {
63                  lastIsCR = true;
64              } else {
65                  if (b == LF && lastIsCR) {
66                      terminatorPos = i;
67                      break;
68                  }
69                  lastIsCR = false;
70              }
71          }
72  
73          if (terminatorPos >= 0) {
74              IoBuffer product;
75  
76              int endPos = terminatorPos - 1;
77  
78              if (beginPos < endPos) {
79                  in.limit(endPos);
80  
81                  if (buffer == null) {
82                      product = in.slice();
83                  } else {
84                      buffer.put(in);
85                      product = buffer.flip();
86                      buffer = null;
87                  }
88  
89                  in.limit(limit);
90              } else {
91                  // When input contained only CR or LF rather than actual data...
92                  if (buffer == null) {
93                      product = IoBuffer.allocate(0);
94                  } else {
95                      product = buffer.flip();
96                      buffer = null;
97                  }
98              }
99              in.position(terminatorPos + 1);
100             return finishDecode(product, out);
101         } else {
102             in.position(beginPos);
103             if (buffer == null) {
104                 buffer = IoBuffer.allocate(in.remaining());
105                 buffer.setAutoExpand(true);
106             }
107 
108             buffer.put(in);
109             if (lastIsCR) {
110                 buffer.position(buffer.position() - 1);
111             }
112             return this;
113         }
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
120         IoBuffer product;
121         // When input contained only CR or LF rather than actual data...
122         if (buffer == null) {
123             product = IoBuffer.allocate(0);
124         } else {
125             product = buffer.flip();
126             buffer = null;
127         }
128         return finishDecode(product, out);
129     }
130 
131     /**
132      * Invoked when this state has reached a <code>CRLF</code>.
133      * 
134      * @param product the read bytes including the <code>CRLF</code>.
135      * @param out the current {@link ProtocolDecoderOutput} used to write 
136      *        decoded messages.
137      * @return the next state if a state transition was triggered (use 
138      *         <code>this</code> for loop transitions) or <code>null</code> if 
139      *         the state machine has reached its end.
140      * @throws Exception if the read data violated protocol specification.
141      */
142     protected abstract DecodingState finishDecode(IoBuffer product,
143             ProtocolDecoderOutput out) throws Exception;
144 }