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