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 received bytes until a configured 27 * number of read bytes has been reached. Please note that this state can 28 * produce a buffer with less data than the configured length if the associated 29 * session has been closed unexpectedly. 30 * 31 * @author The Apache MINA Project (dev@mina.apache.org) 32 * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $ 33 */ 34 public abstract class FixedLengthDecodingState implements DecodingState { 35 36 private final int length; 37 38 private IoBuffer buffer; 39 40 /** 41 * Constructs a new instance using the specified decode length. 42 * 43 * @param length the number of bytes to read. 44 */ 45 public FixedLengthDecodingState(int length) { 46 this.length = length; 47 } 48 49 /** 50 * {@inheritDoc} 51 */ 52 public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) 53 throws Exception { 54 if (buffer == null) { 55 if (in.remaining() >= length) { 56 int limit = in.limit(); 57 in.limit(in.position() + length); 58 IoBuffer product = in.slice(); 59 in.position(in.position() + length); 60 in.limit(limit); 61 return finishDecode(product, out); 62 } else { 63 buffer = IoBuffer.allocate(length); 64 buffer.put(in); 65 return this; 66 } 67 } else { 68 if (in.remaining() >= length - buffer.position()) { 69 int limit = in.limit(); 70 in.limit(in.position() + length - buffer.position()); 71 buffer.put(in); 72 in.limit(limit); 73 IoBuffer product = this.buffer; 74 this.buffer = null; 75 return finishDecode(product.flip(), out); 76 } else { 77 buffer.put(in); 78 return this; 79 } 80 } 81 } 82 83 /** 84 * {@inheritDoc} 85 */ 86 public DecodingState finishDecode(ProtocolDecoderOutput out) 87 throws Exception { 88 IoBuffer readData; 89 if (buffer == null) { 90 readData = IoBuffer.allocate(0); 91 } else { 92 readData = buffer.flip(); 93 buffer = null; 94 } 95 return finishDecode(readData ,out); 96 } 97 98 /** 99 * Invoked when this state has consumed the configured number of bytes. 100 * 101 * @param product the data. 102 * @param out the current {@link ProtocolDecoderOutput} used to write 103 * decoded messages. 104 * @return the next state if a state transition was triggered (use 105 * <code>this</code> for loop transitions) or <code>null</code> if 106 * the state machine has reached its end. 107 * @throws Exception if the read data violated protocol specification. 108 */ 109 protected abstract DecodingState finishDecode(IoBuffer product, 110 ProtocolDecoderOutput out) throws Exception; 111 }