001/**************************************************************** 002 * Licensed to the Apache Software Foundation (ASF) under one * 003 * or more contributor license agreements. See the NOTICE file * 004 * distributed with this work for additional information * 005 * regarding copyright ownership. The ASF licenses this file * 006 * to you under the Apache License, Version 2.0 (the * 007 * "License"); you may not use this file except in compliance * 008 * with the License. You may obtain a copy of the License at * 009 * * 010 * http://www.apache.org/licenses/LICENSE-2.0 * 011 * * 012 * Unless required by applicable law or agreed to in writing, * 013 * software distributed under the License is distributed on an * 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 015 * KIND, either express or implied. See the License for the * 016 * specific language governing permissions and limitations * 017 * under the License. * 018 ****************************************************************/ 019 020package org.apache.james.mime4j.stream; 021 022/** 023 * This class represents a context of a parsing operation: 024 * <ul> 025 * <li>the current position the parsing operation is expected to start at</li> 026 * <li>the bounds limiting the scope of the parsing operation</li> 027 * </ul> 028 * <p/> 029 * Copied from Apache HttpCore project 030 */ 031public class ParserCursor { 032 033 private final int lowerBound; 034 private final int upperBound; 035 private int pos; 036 037 public ParserCursor(int lowerBound, int upperBound) { 038 super(); 039 if (lowerBound < 0) { 040 throw new IndexOutOfBoundsException("Lower bound cannot be negative"); 041 } 042 if (lowerBound > upperBound) { 043 throw new IndexOutOfBoundsException("Lower bound cannot be greater then upper bound"); 044 } 045 this.lowerBound = lowerBound; 046 this.upperBound = upperBound; 047 this.pos = lowerBound; 048 } 049 050 public int getLowerBound() { 051 return this.lowerBound; 052 } 053 054 public int getUpperBound() { 055 return this.upperBound; 056 } 057 058 public int getPos() { 059 return this.pos; 060 } 061 062 public void updatePos(int pos) { 063 if (pos < this.lowerBound) { 064 throw new IndexOutOfBoundsException("pos: "+pos+" < lowerBound: "+this.lowerBound); 065 } 066 if (pos > this.upperBound) { 067 throw new IndexOutOfBoundsException("pos: "+pos+" > upperBound: "+this.upperBound); 068 } 069 this.pos = pos; 070 } 071 072 public boolean atEnd() { 073 return this.pos >= this.upperBound; 074 } 075 076 public String toString() { 077 StringBuilder buffer = new StringBuilder(); 078 buffer.append('['); 079 buffer.append(Integer.toString(this.lowerBound)); 080 buffer.append('>'); 081 buffer.append(Integer.toString(this.pos)); 082 buffer.append('>'); 083 buffer.append(Integer.toString(this.upperBound)); 084 buffer.append(']'); 085 return buffer.toString(); 086 } 087 088}