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.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 DecodingState {
31
32 private int highByte;
33 private int counter;
34
35 public ShortIntegerDecodingState() {
36 }
37
38 public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
39 throws Exception {
40
41 while (in.hasRemaining()) {
42 switch (counter) {
43 case 0:
44 highByte = in.getUnsigned();
45 break;
46 case 1:
47 counter = 0;
48 return finishDecode((short) ((highByte << 8) | in.getUnsigned()), out);
49 default:
50 throw new InternalError();
51 }
52
53 counter ++;
54 }
55 return this;
56 }
57
58 public DecodingState finishDecode(ProtocolDecoderOutput out)
59 throws Exception {
60 throw new ProtocolDecoderException(
61 "Unexpected end of session while waiting for a short integer.");
62 }
63
64 protected abstract DecodingState finishDecode(short value,
65 ProtocolDecoderOutput out) throws Exception;
66 }