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 */ 32 public abstract class ConsumeToEndOfSessionDecodingState implements DecodingState { 33 34 private IoBuffer buffer; 35 private final int maxLength; 36 37 /** 38 * Creates a new instance using the specified maximum length. 39 * 40 * @param maxLength the maximum number of bytes which will be consumed. If 41 * this max is reached a {@link ProtocolDecoderException} will be 42 * thrown by {@link #decode(IoBuffer, ProtocolDecoderOutput)}. 43 */ 44 public ConsumeToEndOfSessionDecodingState(int maxLength) { 45 this.maxLength = maxLength; 46 } 47 48 /** 49 * {@inheritDoc} 50 */ 51 public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) 52 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) 68 throws Exception { 69 try { 70 if (buffer == null) { 71 buffer = IoBuffer.allocate(0); 72 } 73 buffer.flip(); 74 return finishDecode(buffer, out); 75 } finally { 76 buffer = null; 77 } 78 } 79 80 /** 81 * Invoked when this state has consumed all bytes until the session is 82 * closed. 83 * 84 * @param product the bytes read. 85 * @param out the current {@link ProtocolDecoderOutput} used to write 86 * decoded messages. 87 * @return the next state if a state transition was triggered (use 88 * <code>this</code> for loop transitions) or <code>null</code> if 89 * the state machine has reached its end. 90 * @throws Exception if the read data violated protocol specification. 91 */ 92 protected abstract DecodingState finishDecode(IoBuffer product, 93 ProtocolDecoderOutput out) throws Exception; 94 }