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 <a href="http://mina.apache.org">Apache MINA Project</a> 31 */ 32 public abstract class ConsumeToEndOfSessionDecodingState implements DecodingState { 33 34 private IoBuffer buffer; 35 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) throws Exception { 53 if (buffer == null) { 54 buffer = IoBuffer.allocate(256).setAutoExpand(true); 55 } 56 57 if (buffer.position() + in.remaining() > maxLength) { 58 throw new ProtocolDecoderException("Received data exceeds " + maxLength + " byte(s)."); 59 } 60 buffer.put(in); 61 return this; 62 } 63 64 /** 65 * {@inheritDoc} 66 */ 67 public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception { 68 try { 69 if (buffer == null) { 70 buffer = IoBuffer.allocate(0); 71 } 72 buffer.flip(); 73 return finishDecode(buffer, out); 74 } finally { 75 buffer = null; 76 } 77 } 78 79 /** 80 * Invoked when this state has consumed all bytes until the session is 81 * closed. 82 * 83 * @param product the bytes read. 84 * @param out the current {@link ProtocolDecoderOutput} used to write 85 * decoded messages. 86 * @return the next state if a state transition was triggered (use 87 * <code>this</code> for loop transitions) or <code>null</code> if 88 * the state machine has reached its end. 89 * @throws Exception if the read data violated protocol specification. 90 */ 91 protected abstract DecodingState finishDecode(IoBuffer product, ProtocolDecoderOutput out) throws Exception; 92 }