01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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.common.IoBuffer;
23: import org.apache.mina.filter.codec.ProtocolDecoderException;
24: import org.apache.mina.filter.codec.ProtocolDecoderOutput;
25:
26: /**
27: * @author The Apache MINA Project (dev@mina.apache.org)
28: * @version $Rev: 601994 $, $Date: 2007-12-06 21:58:00 -0700 (Thu, 06 Dec 2007) $
29: */
30: public abstract class ShortIntegerDecodingState implements
31: DecodingState {
32:
33: private int highByte;
34: private int counter;
35:
36: public ShortIntegerDecodingState() {
37: }
38:
39: public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
40: throws Exception {
41:
42: while (in.hasRemaining()) {
43: switch (counter) {
44: case 0:
45: highByte = in.getUnsigned();
46: break;
47: case 1:
48: counter = 0;
49: return finishDecode((short) ((highByte << 8) | in
50: .getUnsigned()), out);
51: default:
52: throw new InternalError();
53: }
54:
55: counter++;
56: }
57: return this ;
58: }
59:
60: public DecodingState finishDecode(ProtocolDecoderOutput out)
61: throws Exception {
62: throw new ProtocolDecoderException(
63: "Unexpected end of session while waiting for a short integer.");
64: }
65:
66: protected abstract DecodingState finishDecode(short value,
67: ProtocolDecoderOutput out) throws Exception;
68: }
|