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