1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.filter.codec.statemachine;
21
22 import org.apache.mina.common.IoBuffer;
23 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
24
25
26
27
28
29
30
31 public abstract class SkippingState implements DecodingState {
32
33 private int skippedBytes;
34
35
36
37
38 public SkippingState() {
39 }
40
41 public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
42 throws Exception {
43 int beginPos = in.position();
44 int limit = in.limit();
45 for (int i = beginPos; i < limit; i++) {
46 byte b = in.get(i);
47 if (!canSkip(b)) {
48 in.position(i);
49 int answer = this.skippedBytes;
50 this.skippedBytes = 0;
51 return finishDecode(answer);
52 } else {
53 skippedBytes++;
54 }
55 }
56
57 in.position(limit);
58 return this;
59 }
60
61 public DecodingState finishDecode(ProtocolDecoderOutput out)
62 throws Exception {
63 return finishDecode(skippedBytes);
64 }
65
66 protected abstract boolean canSkip(byte b);
67
68 protected abstract DecodingState finishDecode(int skippedBytes)
69 throws Exception;
70 }