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