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.ProtocolDecoderException; 24 import org.apache.mina.filter.codec.ProtocolDecoderOutput; 25 26 /** 27 * {@link DecodingState} which consumes all received bytes until the session is 28 * closed. 29 * 30 * @author The Apache MINA Project (dev@mina.apache.org) 31 * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $ 32 */ 33 public abstract class ConsumeToEndOfSessionDecodingState implements DecodingState { 34 35 private IoBuffer buffer; 36 private final int maxLength; 37 38 /** 39 * Creates a new instance using the specified maximum length. 40 * 41 * @param maxLength the maximum number of bytes which will be consumed. If 42 * this max is reached a {@link ProtocolDecoderException} will be 43 * thrown by {@link #decode(IoBuffer, ProtocolDecoderOutput)}. 44 */ 45 public ConsumeToEndOfSessionDecodingState(int maxLength) { 46 this.maxLength = maxLength; 47 } 48 49 /** 50 * {@inheritDoc} 51 */ 52 public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) 53 throws Exception { 54 if (buffer == null) { 55 buffer = IoBuffer.allocate(256).setAutoExpand(true); 56 } 57 58 if (buffer.position() + in.remaining() > maxLength) { 59 throw new ProtocolDecoderException("Received data exceeds " + maxLength + " byte(s)."); 60 } 61 buffer.put(in); 62 return this; 63 } 64 65 /** 66 * {@inheritDoc} 67 */ 68 public DecodingState finishDecode(ProtocolDecoderOutput out) 69 throws Exception { 70 try { 71 if (buffer == null) { 72 buffer = IoBuffer.allocate(0); 73 } 74 buffer.flip(); 75 return finishDecode(buffer, out); 76 } finally { 77 buffer = null; 78 } 79 } 80 81 /** 82 * Invoked when this state has consumed all bytes until the session is 83 * closed. 84 * 85 * @param product the bytes read. 86 * @param out the current {@link ProtocolDecoderOutput} used to write 87 * decoded messages. 88 * @return the next state if a state transition was triggered (use 89 * <code>this</code> for loop transitions) or <code>null</code> if 90 * the state machine has reached its end. 91 * @throws Exception if the read data violated protocol specification. 92 */ 93 protected abstract DecodingState finishDecode(IoBuffer product, 94 ProtocolDecoderOutput out) throws Exception; 95 }