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.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      @Override
52      public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) 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  
61                  return finishDecode(product, out);
62              }
63  
64              buffer = IoBuffer.allocate(length);
65              buffer.put(in);
66  
67              return this;
68          }
69  
70          if (in.remaining() >= length - buffer.position()) {
71              int limit = in.limit();
72              in.limit(in.position() + length - buffer.position());
73              buffer.put(in);
74              in.limit(limit);
75              IoBuffer product = this.buffer;
76              this.buffer = null;
77              
78              return finishDecode(product.flip(), out);
79          }
80  
81          buffer.put(in);
82          return this;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
90          IoBuffer readData;
91          
92          if (buffer == null) {
93              readData = IoBuffer.allocate(0);
94          } else {
95              readData = buffer.flip();
96              buffer = null;
97          }
98          
99          return finishDecode(readData, out);
100     }
101 
102     /**
103      * Invoked when this state has consumed the configured number of bytes.
104      * 
105      * @param product the data.
106      * @param out the current {@link ProtocolDecoderOutput} used to write 
107      *        decoded messages.
108      * @return the next state if a state transition was triggered (use 
109      *         <code>this</code> for loop transitions) or <code>null</code> if 
110      *         the state machine has reached its end.
111      * @throws Exception if the read data violated protocol specification.
112      */
113     protected abstract DecodingState finishDecode(IoBuffer product, ProtocolDecoderOutput out) throws Exception;
114 }