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 ConsumeToDynamicTerminatorDecodingState implements
033: DecodingState {
034:
035: private IoBuffer buffer;
036:
037: /**
038: * Creates a new instance.
039: */
040: public ConsumeToDynamicTerminatorDecodingState() {
041: }
042:
043: public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out)
044: throws Exception {
045: int beginPos = in.position();
046: int terminatorPos = -1;
047: int limit = in.limit();
048:
049: for (int i = beginPos; i < limit; i++) {
050: byte b = in.get(i);
051: if (isTerminator(b)) {
052: terminatorPos = i;
053: break;
054: }
055: }
056:
057: if (terminatorPos >= 0) {
058: IoBuffer product;
059:
060: if (beginPos < terminatorPos) {
061: in.limit(terminatorPos);
062:
063: if (buffer == null) {
064: product = in.slice();
065: } else {
066: buffer.put(in);
067: product = buffer.flip();
068: buffer = null;
069: }
070:
071: in.limit(limit);
072: } else {
073: // When input contained only terminator rather than actual data...
074: if (buffer == null) {
075: product = IoBuffer.allocate(0);
076: } else {
077: product = buffer.flip();
078: buffer = null;
079: }
080: }
081: in.position(terminatorPos + 1);
082: return finishDecode(product, out);
083: } else {
084: if (buffer == null) {
085: buffer = IoBuffer.allocate(in.remaining());
086: buffer.setAutoExpand(true);
087: }
088: buffer.put(in);
089: return this ;
090: }
091: }
092:
093: public DecodingState finishDecode(ProtocolDecoderOutput out)
094: throws Exception {
095: IoBuffer product;
096: // When input contained only terminator rather than actual data...
097: if (buffer == null) {
098: product = IoBuffer.allocate(0);
099: } else {
100: product = buffer.flip();
101: buffer = null;
102: }
103: return finishDecode(product, out);
104: }
105:
106: protected abstract boolean isTerminator(byte b);
107:
108: protected abstract DecodingState finishDecode(IoBuffer product,
109: ProtocolDecoderOutput out) throws Exception;
110: }
|