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 {@link #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      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
39              throws Exception {
40          int beginPos = in.position();
41          int limit = in.limit();
42          for (int i = beginPos; i < limit; i++) {
43              byte b = in.get(i);
44              if (!canSkip(b)) {
45                  in.position(i);
46                  int answer = this.skippedBytes;
47                  this.skippedBytes = 0;
48                  return finishDecode(answer);
49              }
50              
51              skippedBytes++;
52          }
53  
54          in.position(limit);
55          return this;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public DecodingState finishDecode(ProtocolDecoderOutput out)
62              throws Exception {
63          return finishDecode(skippedBytes);
64      }
65  
66      /**
67       * Called to determine whether the specified byte can be skipped.
68       * 
69       * @param b the byte to check.
70       * @return <code>true</code> if the byte can be skipped.
71       */
72      protected abstract boolean canSkip(byte b);
73  
74      /**
75       * Invoked when this state cannot skip any more bytes.
76       * 
77       * @param skippedBytes the number of bytes skipped.
78       * @return the next state if a state transition was triggered (use 
79       *         <code>this</code> for loop transitions) or <code>null</code> if 
80       *         the state machine has reached its end.
81       * @throws Exception if the read data violated protocol specification.
82       */
83      protected abstract DecodingState finishDecode(int skippedBytes)
84              throws Exception;
85  }