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.james.mime4j.stream; |
---|
21 | |
---|
22 | /** |
---|
23 | * This class represents a context of a parsing operation: |
---|
24 | * <ul> |
---|
25 | * <li>the current position the parsing operation is expected to start at</li> |
---|
26 | * <li>the bounds limiting the scope of the parsing operation</li> |
---|
27 | * </ul> |
---|
28 | * <p/> |
---|
29 | * Copied from Apache HttpCore project |
---|
30 | */ |
---|
31 | class ParserCursor { |
---|
32 | |
---|
33 | private final int lowerBound; |
---|
34 | private final int upperBound; |
---|
35 | private int pos; |
---|
36 | |
---|
37 | public ParserCursor(int lowerBound, int upperBound) { |
---|
38 | super(); |
---|
39 | if (lowerBound < 0) { |
---|
40 | throw new IndexOutOfBoundsException("Lower bound cannot be negative"); |
---|
41 | } |
---|
42 | if (lowerBound > upperBound) { |
---|
43 | throw new IndexOutOfBoundsException("Lower bound cannot be greater then upper bound"); |
---|
44 | } |
---|
45 | this.lowerBound = lowerBound; |
---|
46 | this.upperBound = upperBound; |
---|
47 | this.pos = lowerBound; |
---|
48 | } |
---|
49 | |
---|
50 | public int getLowerBound() { |
---|
51 | return this.lowerBound; |
---|
52 | } |
---|
53 | |
---|
54 | public int getUpperBound() { |
---|
55 | return this.upperBound; |
---|
56 | } |
---|
57 | |
---|
58 | public int getPos() { |
---|
59 | return this.pos; |
---|
60 | } |
---|
61 | |
---|
62 | public void updatePos(int pos) { |
---|
63 | if (pos < this.lowerBound) { |
---|
64 | throw new IndexOutOfBoundsException("pos: "+pos+" < lowerBound: "+this.lowerBound); |
---|
65 | } |
---|
66 | if (pos > this.upperBound) { |
---|
67 | throw new IndexOutOfBoundsException("pos: "+pos+" > upperBound: "+this.upperBound); |
---|
68 | } |
---|
69 | this.pos = pos; |
---|
70 | } |
---|
71 | |
---|
72 | public boolean atEnd() { |
---|
73 | return this.pos >= this.upperBound; |
---|
74 | } |
---|
75 | |
---|
76 | public String toString() { |
---|
77 | StringBuilder buffer = new StringBuilder(); |
---|
78 | buffer.append('['); |
---|
79 | buffer.append(Integer.toString(this.lowerBound)); |
---|
80 | buffer.append('>'); |
---|
81 | buffer.append(Integer.toString(this.pos)); |
---|
82 | buffer.append('>'); |
---|
83 | buffer.append(Integer.toString(this.upperBound)); |
---|
84 | buffer.append(']'); |
---|
85 | return buffer.toString(); |
---|
86 | } |
---|
87 | |
---|
88 | } |
---|