View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2011 Michael Mimo Moratti.
3    *
4    * Michael Mimo Moratti licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at:
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
11   * License for the specific language governing permissions and limitations
12   * under the License.
13   *******************************************************************************/
14  package ch.mimo.netty.handler.codec.icap;
15  
16  import org.jboss.netty.handler.codec.frame.TooLongFrameException;
17  
18  /**
19   * This class is used to track the size in bytes of headers.
20   * 
21   * @author Michael Mimo Moratti (mimo@mimo.ch)
22   *
23   * @see IcapDecoderUtil
24   * @see ReadTrailingHeadersState
25   */
26  public class SizeDelimiter {
27  
28  	private int counter = 0;
29  	private int limit;
30  	private String errorMessage;
31  	
32  	public SizeDelimiter(int limit) {
33  		this.limit = limit;
34  		this.errorMessage = "limit exeeded by: ";
35  	}
36  	
37  	public synchronized void increment(int count) throws DecodingException {
38  		counter += count;
39  		checkLimit();
40  	}
41  	
42  	public void increment() throws DecodingException {
43  		this.increment(1);
44  	}
45  	
46  	public int getSize() {
47  		return counter;
48  	}
49  	
50  	private void checkLimit() throws DecodingException {
51  		if(counter >= limit) {
52  			throw new DecodingException(new TooLongFrameException(errorMessage + "[" + (counter - limit) + "] counts"));
53  		}
54  	}
55  }