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 bytes until a fixed (ASCII) 
027 * character is reached. The terminator is skipped.
028 *
029 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
030 */
031public abstract class ConsumeToDynamicTerminatorDecodingState implements DecodingState {
032
033    private IoBuffer buffer;
034
035    /**
036     * {@inheritDoc}
037     */
038    public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
039        int beginPos = in.position();
040        int terminatorPos = -1;
041        int limit = in.limit();
042
043        for (int i = beginPos; i < limit; i++) {
044            byte b = in.get(i);
045            if (isTerminator(b)) {
046                terminatorPos = i;
047                break;
048            }
049        }
050
051        if (terminatorPos >= 0) {
052            IoBuffer product;
053
054            if (beginPos < terminatorPos) {
055                in.limit(terminatorPos);
056
057                if (buffer == null) {
058                    product = in.slice();
059                } else {
060                    buffer.put(in);
061                    product = buffer.flip();
062                    buffer = null;
063                }
064
065                in.limit(limit);
066            } else {
067                // When input contained only terminator rather than actual data...
068                if (buffer == null) {
069                    product = IoBuffer.allocate(0);
070                } else {
071                    product = buffer.flip();
072                    buffer = null;
073                }
074            }
075            in.position(terminatorPos + 1);
076            return finishDecode(product, out);
077        }
078
079        if (buffer == null) {
080            buffer = IoBuffer.allocate(in.remaining());
081            buffer.setAutoExpand(true);
082        }
083        buffer.put(in);
084        return this;
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
091        IoBuffer product;
092        // When input contained only terminator rather than actual data...
093        if (buffer == null) {
094            product = IoBuffer.allocate(0);
095        } else {
096            product = buffer.flip();
097            buffer = null;
098        }
099        return finishDecode(product, out);
100    }
101
102    /**
103     * Determines whether the specified <code>byte</code> is a terminator.
104     * 
105     * @param b the <code>byte</code> to check.
106     * @return <tt>true</tt> if <code>b</code> is a terminator, 
107     *         <tt>false</tt> otherwise.
108     */
109    protected abstract boolean isTerminator(byte b);
110
111    /**
112     * Invoked when this state has reached the terminator byte.
113     * 
114     * @param product the read bytes not including the terminator.
115     * @param out the current {@link ProtocolDecoderOutput} used to write 
116     *        decoded messages.
117     * @return the next state if a state transition was triggered (use 
118     *         <code>this</code> for loop transitions) or <code>null</code> if 
119     *         the state machine has reached its end.
120     * @throws Exception if the read data violated protocol specification.
121     */
122    protected abstract DecodingState finishDecode(IoBuffer product, ProtocolDecoderOutput out) throws Exception;
123}