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