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 bytes until a fixed (ASCII) 27 * character is reached. The terminator is skipped. 28 * 29 * @author The Apache MINA Project (dev@mina.apache.org) 30 * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $ 31 */ 32 public abstract class ConsumeToTerminatorDecodingState implements DecodingState { 33 34 private final byte terminator; 35 36 private IoBuffer buffer; 37 38 /** 39 * Creates a new instance using the specified terminator character. 40 * 41 * @param terminator the terminator character. 42 */ 43 public ConsumeToTerminatorDecodingState(byte terminator) { 44 this.terminator = terminator; 45 } 46 47 /** 48 * {@inheritDoc} 49 */ 50 public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) 51 throws Exception { 52 int terminatorPos = in.indexOf(terminator); 53 54 if (terminatorPos >= 0) { 55 int limit = in.limit(); 56 IoBuffer product; 57 58 if (in.position() < terminatorPos) { 59 in.limit(terminatorPos); 60 61 if (buffer == null) { 62 product = in.slice(); 63 } else { 64 buffer.put(in); 65 product = buffer.flip(); 66 buffer = null; 67 } 68 69 in.limit(limit); 70 } else { 71 // When input contained only terminator rather than actual data... 72 if (buffer == null) { 73 product = IoBuffer.allocate(0); 74 } else { 75 product = buffer.flip(); 76 buffer = null; 77 } 78 } 79 in.position(terminatorPos + 1); 80 return finishDecode(product, out); 81 } else { 82 if (buffer == null) { 83 buffer = IoBuffer.allocate(in.remaining()); 84 buffer.setAutoExpand(true); 85 } 86 buffer.put(in); 87 return this; 88 } 89 } 90 91 /** 92 * {@inheritDoc} 93 */ 94 public DecodingState finishDecode(ProtocolDecoderOutput out) 95 throws Exception { 96 IoBuffer product; 97 // When input contained only terminator rather than actual data... 98 if (buffer == null) { 99 product = IoBuffer.allocate(0); 100 } else { 101 product = buffer.flip(); 102 buffer = null; 103 } 104 return finishDecode(product, out); 105 } 106 107 /** 108 * Invoked when this state has reached the terminator byte. 109 * 110 * @param product the read bytes not including the terminator. 111 * @param out the current {@link ProtocolDecoderOutput} used to write 112 * decoded messages. 113 * @return the next state if a state transition was triggered (use 114 * <code>this</code> for loop transitions) or <code>null</code> if 115 * the state machine has reached its end. 116 * @throws Exception if the read data violated protocol specification. 117 */ 118 protected abstract DecodingState finishDecode(IoBuffer product, 119 ProtocolDecoderOutput out) throws Exception; 120 }