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 skips data until canSkip(byte) returns 
27   * <tt>false</tt>.
28   *
29   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
30   */
31  public abstract class SkippingState implements DecodingState {
32  
33      private int skippedBytes;
34  
35      /**
36       * {@inheritDoc}
37       */
38      @Override
39      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
40          int beginPos = in.position();
41          int limit = in.limit();
42          
43          for (int i = beginPos; i < limit; i++) {
44              byte b = in.get(i);
45              
46              if (!canSkip(b)) {
47                  in.position(i);
48                  int answer = this.skippedBytes;
49                  this.skippedBytes = 0;
50                  
51                  return finishDecode(answer);
52              }
53  
54              skippedBytes++;
55          }
56  
57          in.position(limit);
58          return this;
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
66          return finishDecode(skippedBytes);
67      }
68  
69      /**
70       * Called to determine whether the specified byte can be skipped.
71       * 
72       * @param b the byte to check.
73       * @return <tt>true</tt> if the byte can be skipped.
74       */
75      protected abstract boolean canSkip(byte b);
76  
77      /**
78       * Invoked when this state cannot skip any more bytes.
79       * 
80       * @param skippedBytes the number of bytes skipped.
81       * @return the next state if a state transition was triggered (use 
82       *         <code>this</code> for loop transitions) or <code>null</code> if 
83       *         the state machine has reached its end.
84       * @throws Exception if the read data violated protocol specification.
85       */
86      protected abstract DecodingState finishDecode(int skippedBytes) throws Exception;
87  }