1 | public char charAt(int index) {↵ | | 1 | public char charAt(int index) {↵
|
2 | if (index == 0) {↵ | | 2 | if (index == 0) {↵
|
3 | return cached;↵ | | 3 | return cached;↵
|
4 | } else if (index >= end) {↵ | | 4 | } else if (index >= end) {↵
|
5 | return OUT_OF_BOUNDS;↵ | | 5 | return OUT_OF_BOUNDS;↵
|
6 | } else if (index == -1) {↵ | | 6 | ↵
|
7 | return lookBehind[0];↵ | | |
|
8 | } else if (index == -2) {↵ | | |
|
9 | return lookBehind[1];↵ | | |
|
10 | } else if (index < -2) {↵ | | |
|
11 | return OUT_OF_BOUNDS;↵ | | |
|
12 | } else if (index >= bufsize) {↵ | | 7 | } else if (index >= bufsize) {↵
|
13 | // Allocate more space in the buffer.↵ | | 8 | // Allocate more space in the buffer.↵
|
14 | try {↵ | | 9 | try {↵
|
15 | while (bufsize <= index) bufsize += BUFFER_INCREMENT;↵ | | 10 | while (bufsize <= index) bufsize += BUFFER_INCREMENT;↵
|
16 | br.reset();↵ | | 11 | br.reset();↵
|
17 | br.mark(bufsize);↵ | | 12 | br.mark(bufsize);↵
|
18 | br.skip(index-1);↵ | | 13 | br.skip(index-1);↵
|
19 | } catch (IOException e) { }↵ | | 14 | } catch (IOException e) { }↵
|
20 | } else if (this.index != index) {↵ | | 15 | } else if (this.index != index) {↵
|
21 | try {↵ | | 16 | try {↵
|
22 | br.reset();↵ | | 17 | br.reset();↵
|
23 | br.skip(index-1);↵ | | 18 | br.skip(index-1);↵
|
24 | } catch (IOException e) { }↵ | | 19 | } catch (IOException e) { }↵
|
25 | }↵ | | 20 | } else if (index == -1) {↵
|
| | | 21 | return lookBehind[0];↵
|
| | | 22 | } else if (index == -2) {↵
|
| | | 23 | return lookBehind[1];↵
|
| | | 24 | } else if (index < -2) {↵
|
| | | 25 | return OUT_OF_BOUNDS;↵
|
| | | 26 | }↵
|
|
26 | char ch = OUT_OF_BOUNDS;↵ | | 27 | char ch = OUT_OF_BOUNDS;↵
|
27 | ↵ | | 28 | ↵
|
28 | try {↵ | | 29 | try {↵
|
29 | int i = br.read();↵ | | 30 | int i = br.read();↵
|
30 | this.index = index+1; // this.index is index of next pos relative to charAt(0)↵ | | 31 | this.index = index+1; // this.index is index of next pos relative to charAt(0)↵
|
31 | if (i == -1) {↵ | | 32 | if (i == -1) {↵
|
32 | // set flag that next should fail next time?↵ | | 33 | // set flag that next should fail next time?↵
|
33 | end = index;↵ | | 34 | end = index;↵
|
34 | return ch;↵ | | 35 | return ch;↵
|
35 | }↵ | | 36 | }↵
|
36 | ch = (char) i;↵ | | 37 | ch = (char) i;↵
|
37 | } catch (IOException ie) { }↵ | | 38 | } catch (IOException ie) { }↵
|
38 | ↵ | | 39 | ↵
|
39 | return ch;↵ | | 40 | return ch;↵
|
40 | | | 41 |
|