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