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 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) throws Exception {
50          int terminatorPos = in.indexOf(terminator);
51  
52          if (terminatorPos >= 0) {
53              int limit = in.limit();
54              IoBuffer product;
55  
56              if (in.position() < 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  
86          buffer.put(in);
87          return this;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public DecodingState finishDecode(ProtocolDecoderOutput out) 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      * Invoked when this state has reached the terminator byte.
107      * 
108      * @param product the read bytes not including the terminator.
109      * @param out the current {@link ProtocolDecoderOutput} used to write 
110      *        decoded messages.
111      * @return the next state if a state transition was triggered (use 
112      *         <code>this</code> for loop transitions) or <code>null</code> if 
113      *         the state machine has reached its end.
114      * @throws Exception if the read data violated protocol specification.
115      */
116     protected abstract DecodingState finishDecode(IoBuffer product, ProtocolDecoderOutput out) throws Exception;
117 }