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 java.util.Queue;
23 import java.util.concurrent.ConcurrentLinkedQueue;
24
25 import org.apache.mina.core.buffer.IoBuffer;
26 import org.apache.mina.core.session.IoSession;
27 import org.apache.mina.filter.codec.ProtocolDecoder;
28 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
29
30
31
32
33
34
35
36
37
38
39
40 public class DecodingStateProtocolDecoder implements ProtocolDecoder {
41 private final DecodingState state;
42
43 private final Queue<IoBuffer> undecodedBuffers = new ConcurrentLinkedQueue<IoBuffer>();
44
45 private IoSession session;
46
47
48
49
50
51
52
53
54 public DecodingStateProtocolDecoder(DecodingState state) {
55 if (state == null) {
56 throw new IllegalArgumentException("state");
57 }
58 this.state = state;
59 }
60
61
62
63
64 public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
65 if (this.session == null) {
66 this.session = session;
67 } else if (this.session != session) {
68 throw new IllegalStateException(getClass().getSimpleName() + " is a stateful decoder. "
69 + "You have to create one per session.");
70 }
71
72 undecodedBuffers.offer(in);
73 for (;;) {
74 IoBuffer b = undecodedBuffers.peek();
75 if (b == null) {
76 break;
77 }
78
79 int oldRemaining = b.remaining();
80 state.decode(b, out);
81 int newRemaining = b.remaining();
82 if (newRemaining != 0) {
83 if (oldRemaining == newRemaining) {
84 throw new IllegalStateException(DecodingState.class.getSimpleName() + " must "
85 + "consume at least one byte per decode().");
86 }
87 } else {
88 undecodedBuffers.poll();
89 }
90 }
91 }
92
93
94
95
96 public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
97 state.finishDecode(out);
98 }
99
100
101
102
103 public void dispose(IoSession session) throws Exception {
104
105 }
106 }