01: package org.archive.io;
02:
03: import java.io.IOException;
04:
05: public class SeekReaderCharSequence implements CharSequence {
06:
07: final private SeekReader reader;
08: final private int size;
09:
10: public SeekReaderCharSequence(SeekReader reader, int size) {
11: this .reader = reader;
12: this .size = size;
13: }
14:
15: public int length() {
16: return size;
17: }
18:
19: public char charAt(int index) {
20: if ((index < 0) || (index >= length())) {
21: throw new IndexOutOfBoundsException(Integer.toString(index));
22: }
23: try {
24: reader.position(index);
25: int r = reader.read();
26: if (r < 0) {
27: throw new IllegalStateException("EOF");
28: }
29: return (char) reader.read();
30: } catch (IOException e) {
31: throw new RuntimeException(e);
32: }
33: }
34:
35: public CharSequence subSequence(int start, int end) {
36: return new CharSubSequence(this , start, end);
37: }
38:
39: public String toString() {
40: StringBuilder sb = new StringBuilder();
41: try {
42: reader.position(0);
43: for (int ch = reader.read(); ch >= 0; ch = reader.read()) {
44: sb.append((char) ch);
45: }
46: return sb.toString();
47: } catch (IOException e) {
48: throw new IllegalStateException(e);
49: }
50: }
51: }
|