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: */
020: package org.apache.mina.filter.codec.statemachine;
021:
022: import org.apache.mina.common.IoBuffer;
023: import org.apache.mina.filter.codec.ProtocolDecoderOutput;
024:
025: /**
026: * Consumes until a fixed (ASCII) character is reached.
027: * The terminator is skipped.
028: *
029: * @author The Apache MINA Project (dev@mina.apache.org)
030: * @version $Rev: 601994 $, $Date: 2007-12-06 21:58:00 -0700 (Thu, 06 Dec 2007) $
031: */
032: public abstract class ConsumeToTerminatorDecodingState implements
033: DecodingState {
034:
035: private final byte terminator;
036:
037: private IoBuffer buffer;
038:
039: /**
040: * @param terminator The terminator character
041: */
042: public ConsumeToTerminatorDecodingState(byte terminator) {
043: this .terminator = terminator;
044: }
045:
046: public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
047: throws Exception {
048: int terminatorPos = in.indexOf(terminator);
049:
050: if (terminatorPos >= 0) {
051: int limit = in.limit();
052: IoBuffer product;
053:
054: if (in.position() < 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: } else {
078: if (buffer == null) {
079: buffer = IoBuffer.allocate(in.remaining());
080: buffer.setAutoExpand(true);
081: }
082: buffer.put(in);
083: return this ;
084: }
085: }
086:
087: public DecodingState finishDecode(ProtocolDecoderOutput out)
088: throws Exception {
089: IoBuffer product;
090: // When input contained only terminator rather than actual data...
091: if (buffer == null) {
092: product = IoBuffer.allocate(0);
093: } else {
094: product = buffer.flip();
095: buffer = null;
096: }
097: return finishDecode(product, out);
098: }
099:
100: protected abstract DecodingState finishDecode(IoBuffer product,
101: ProtocolDecoderOutput out) throws Exception;
102: }
|