001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.codec.statemachine;
021
022import org.apache.mina.core.buffer.IoBuffer;
023import org.apache.mina.filter.codec.ProtocolDecoderOutput;
024
025/**
026 * {@link DecodingState} which consumes all received bytes until a configured
027 * number of read bytes has been reached. Please note that this state can
028 * produce a buffer with less data than the configured length if the associated 
029 * session has been closed unexpectedly.
030 *
031 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
032 */
033public abstract class FixedLengthDecodingState implements DecodingState {
034
035    private final int length;
036
037    private IoBuffer buffer;
038
039    /**
040     * Constructs a new instance using the specified decode length.
041     *
042     * @param length the number of bytes to read.
043     */
044    public FixedLengthDecodingState(int length) {
045        this.length = length;
046    }
047
048    /**
049     * {@inheritDoc}
050     */
051    public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
052        if (buffer == null) {
053            if (in.remaining() >= length) {
054                int limit = in.limit();
055                in.limit(in.position() + length);
056                IoBuffer product = in.slice();
057                in.position(in.position() + length);
058                in.limit(limit);
059                return finishDecode(product, out);
060            }
061
062            buffer = IoBuffer.allocate(length);
063            buffer.put(in);
064            return this;
065        }
066
067        if (in.remaining() >= length - buffer.position()) {
068            int limit = in.limit();
069            in.limit(in.position() + length - buffer.position());
070            buffer.put(in);
071            in.limit(limit);
072            IoBuffer product = this.buffer;
073            this.buffer = null;
074            return finishDecode(product.flip(), out);
075        }
076
077        buffer.put(in);
078        return this;
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
085        IoBuffer readData;
086        if (buffer == null) {
087            readData = IoBuffer.allocate(0);
088        } else {
089            readData = buffer.flip();
090            buffer = null;
091        }
092        return finishDecode(readData, out);
093    }
094
095    /**
096     * Invoked when this state has consumed the configured number of bytes.
097     * 
098     * @param product the data.
099     * @param out the current {@link ProtocolDecoderOutput} used to write 
100     *        decoded messages.
101     * @return the next state if a state transition was triggered (use 
102     *         <code>this</code> for loop transitions) or <code>null</code> if 
103     *         the state machine has reached its end.
104     * @throws Exception if the read data violated protocol specification.
105     */
106    protected abstract DecodingState finishDecode(IoBuffer product, ProtocolDecoderOutput out) throws Exception;
107}