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 consumes all received bytes until a configured
27 * number of read bytes has been reached. Please note that this state can
28 * produce a buffer with less data than the configured length if the associated
29 * session has been closed unexpectedly.
30 *
31 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
32 */
33 public abstract class FixedLengthDecodingState implements DecodingState {
34
35 private final int length;
36
37 private IoBuffer buffer;
38
39 /**
40 * Constructs a new instance using the specified decode length.
41 *
42 * @param length the number of bytes to read.
43 */
44 public FixedLengthDecodingState(int length) {
45 this.length = length;
46 }
47
48 /**
49 * {@inheritDoc}
50 */
51 public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
52 throws Exception {
53 if (buffer == null) {
54 if (in.remaining() >= length) {
55 int limit = in.limit();
56 in.limit(in.position() + length);
57 IoBuffer product = in.slice();
58 in.position(in.position() + length);
59 in.limit(limit);
60 return finishDecode(product, out);
61 }
62
63 buffer = IoBuffer.allocate(length);
64 buffer.put(in);
65 return this;
66 }
67
68 if (in.remaining() >= length - buffer.position()) {
69 int limit = in.limit();
70 in.limit(in.position() + length - buffer.position());
71 buffer.put(in);
72 in.limit(limit);
73 IoBuffer product = this.buffer;
74 this.buffer = null;
75 return finishDecode(product.flip(), out);
76 }
77
78 buffer.put(in);
79 return this;
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 public DecodingState finishDecode(ProtocolDecoderOutput out)
86 throws Exception {
87 IoBuffer readData;
88 if (buffer == null) {
89 readData = IoBuffer.allocate(0);
90 } else {
91 readData = buffer.flip();
92 buffer = null;
93 }
94 return finishDecode(readData ,out);
95 }
96
97 /**
98 * Invoked when this state has consumed the configured number of bytes.
99 *
100 * @param product the data.
101 * @param out the current {@link ProtocolDecoderOutput} used to write
102 * decoded messages.
103 * @return the next state if a state transition was triggered (use
104 * <code>this</code> for loop transitions) or <code>null</code> if
105 * the state machine has reached its end.
106 * @throws Exception if the read data violated protocol specification.
107 */
108 protected abstract DecodingState finishDecode(IoBuffer product,
109 ProtocolDecoderOutput out) throws Exception;
110 }