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 ConsumeToTerminatorDecodingState implements DecodingState {
32  
33      private final byte terminator;
34  
35      private IoBuffer buffer;
36  
37      /**
38       * Creates a new instance using the specified terminator character.
39       * 
40       * @param terminator the terminator character.
41       */
42      public ConsumeToTerminatorDecodingState(byte terminator) {
43          this.terminator = terminator;
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
50              throws Exception {
51          int terminatorPos = in.indexOf(terminator);
52  
53          if (terminatorPos >= 0) {
54              int limit = in.limit();
55              IoBuffer product;
56  
57              if (in.position() < 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          }
81  
82          if (buffer == null) {
83              buffer = IoBuffer.allocate(in.remaining());
84              buffer.setAutoExpand(true);
85          }
86          
87          buffer.put(in);
88          return this;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      public DecodingState finishDecode(ProtocolDecoderOutput out)
95              throws Exception {
96          IoBuffer product;
97          // When input contained only terminator rather than actual data...
98          if (buffer == null) {
99              product = IoBuffer.allocate(0);
100         } else {
101             product = buffer.flip();
102             buffer = null;
103         }
104         return finishDecode(product, out);
105     }
106 
107     /**
108      * Invoked when this state has reached the terminator byte.
109      * 
110      * @param product the read bytes not including the terminator.
111      * @param out the current {@link ProtocolDecoderOutput} used to write 
112      *        decoded messages.
113      * @return the next state if a state transition was triggered (use 
114      *         <code>this</code> for loop transitions) or <code>null</code> if 
115      *         the state machine has reached its end.
116      * @throws Exception if the read data violated protocol specification.
117      */
118     protected abstract DecodingState finishDecode(IoBuffer product,
119             ProtocolDecoderOutput out) throws Exception;
120 }