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