View Javadoc

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.ProtocolDecoderOutput;
24  
25  /**
26   * A {@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 the buffer with less data if the associated session has been
29   * closed unexpectedly.
30   *
31   * @author The Apache MINA Project (dev@mina.apache.org)
32   * @version $Rev: 601994 $, $Date: 2007-12-06 21:58:00 -0700 (Thu, 06 Dec 2007) $
33   */
34  public abstract class FixedLengthDecodingState implements DecodingState {
35  
36      private final int length;
37  
38      private IoBuffer buffer;
39  
40      /**
41       * Constructs with a known decode length.
42       *
43       * @param length    The decode length
44       */
45      public FixedLengthDecodingState(int length) {
46          this.length = length;
47      }
48  
49      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
50              throws Exception {
51          if (buffer == null) {
52              if (in.remaining() >= length) {
53                  int limit = in.limit();
54                  in.limit(in.position() + length);
55                  IoBuffer product = in.slice();
56                  in.position(in.position() + length);
57                  in.limit(limit);
58                  return finishDecode(product, out);
59              } else {
60                  buffer = IoBuffer.allocate(length);
61                  buffer.put(in);
62                  return this;
63              }
64          } else {
65              if (in.remaining() >= length - buffer.position()) {
66                  int limit = in.limit();
67                  in.limit(in.position() + length - buffer.position());
68                  buffer.put(in);
69                  in.limit(limit);
70                  IoBuffer product = this.buffer;
71                  this.buffer = null;
72                  return finishDecode(product.flip(), out);
73              } else {
74                  buffer.put(in);
75                  return this;
76              }
77          }
78      }
79  
80      public DecodingState finishDecode(ProtocolDecoderOutput out)
81              throws Exception {
82          IoBuffer readData;
83          if (buffer == null) {
84              readData = IoBuffer.allocate(0);
85          } else {
86              readData = buffer.flip();
87              buffer = null;
88          }
89          return finishDecode(readData ,out);
90      }
91  
92      protected abstract DecodingState finishDecode(IoBuffer readData,
93              ProtocolDecoderOutput out) throws Exception;
94  }