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 fixed (ASCII) 
27   * character is reached. The terminator is skipped.
28   *
29   * @author The Apache MINA Project (dev@mina.apache.org)
30   */
31  public abstract class ConsumeToDynamicTerminatorDecodingState implements
32          DecodingState {
33  
34      private IoBuffer buffer;
35  
36      /**
37       * {@inheritDoc}
38       */
39      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
40              throws Exception {
41          int beginPos = in.position();
42          int terminatorPos = -1;
43          int limit = in.limit();
44  
45          for (int i = beginPos; i < limit; i++) {
46              byte b = in.get(i);
47              if (isTerminator(b)) {
48                  terminatorPos = i;
49                  break;
50              }
51          }
52  
53          if (terminatorPos >= 0) {
54              IoBuffer product;
55  
56              if (beginPos < terminatorPos) {
57                  in.limit(terminatorPos);
58  
59                  if (buffer == null) {
60                      product = in.slice();
61                  } else {
62                      buffer.put(in);
63                      product = buffer.flip();
64                      buffer = null;
65                  }
66  
67                  in.limit(limit);
68              } else {
69                  // When input contained only terminator rather than actual data...
70                  if (buffer == null) {
71                      product = IoBuffer.allocate(0);
72                  } else {
73                      product = buffer.flip();
74                      buffer = null;
75                  }
76              }
77              in.position(terminatorPos + 1);
78              return finishDecode(product, out);
79          }
80          
81          if (buffer == null) {
82              buffer = IoBuffer.allocate(in.remaining());
83              buffer.setAutoExpand(true);
84          }
85          buffer.put(in);
86          return this;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public DecodingState finishDecode(ProtocolDecoderOutput out)
93              throws Exception {
94          IoBuffer product;
95          // When input contained only terminator rather than actual data...
96          if (buffer == null) {
97              product = IoBuffer.allocate(0);
98          } else {
99              product = buffer.flip();
100             buffer = null;
101         }
102         return finishDecode(product, out);
103     }
104 
105     /**
106      * Determines whether the specified <code>byte</code> is a terminator.
107      * 
108      * @param b the <code>byte</code> to check.
109      * @return <code>true</code> if <code>b</code> is a terminator, 
110      *         <code>false</code> otherwise.
111      */
112     protected abstract boolean isTerminator(byte b);
113 
114     /**
115      * Invoked when this state has reached the terminator byte.
116      * 
117      * @param product the read bytes not including the terminator.
118      * @param out the current {@link ProtocolDecoderOutput} used to write 
119      *        decoded messages.
120      * @return the next state if a state transition was triggered (use 
121      *         <code>this</code> for loop transitions) or <code>null</code> if 
122      *         the state machine has reached its end.
123      * @throws Exception if the read data violated protocol specification.
124      */
125     protected abstract DecodingState finishDecode(IoBuffer product,
126             ProtocolDecoderOutput out) throws Exception;
127 }