01: package gnu.jemacs.buffer;
02:
03: import gnu.mapping.*;
04: import java.io.*;
05: import gnu.lists.CharBuffer;
06: import gnu.text.*;
07:
08: public class BufferReader extends InPort {
09: CharBuffer content;
10: int rangeStart;
11: int rangeLength;
12:
13: public BufferReader(CharBuffer content, Path path, int start,
14: int count) {
15: super (gnu.text.NullReader.nullReader, path);
16: this .content = content;
17: buffer = content.getArray();
18: rangeStart = start;
19: rangeLength = count;
20: if (start < content.gapStart) {
21: pos = start;
22: limit = start + count;
23: if (limit > content.gapStart)
24: limit = content.gapStart;
25: } else {
26: int gapSize = content.gapEnd - content.gapStart;
27: pos = start + gapSize;
28: int length = content.getArray().length;
29: limit = pos + count > length ? length : pos + count;
30: }
31: }
32:
33: public int read() {
34: if (pos < limit)
35: return buffer[pos++];
36: if (limit == content.gapStart) {
37: int gapSize = content.gapEnd - content.gapStart;
38: pos = content.gapEnd;
39: int count = rangeLength - (content.gapStart - rangeStart);
40: int length = content.getArray().length;
41: limit = pos + count > length ? length : pos + count;
42: if (pos < limit)
43: return buffer[pos++];
44: }
45: return -1;
46: }
47:
48: // int highestPos - is not used
49: // public synchronized void mark (int readAheadLimit) - seems OK.
50: // public boolean ready () -- seems OK
51:
52: public void reset() throws IOException {
53: if (readAheadLimit <= 0)
54: throw new IOException("mark invalid");
55: if (pos >= content.gapEnd && markPos <= content.gapStart)
56: limit = content.gapEnd;
57: pos = markPos;
58: readAheadLimit = 0;
59: }
60:
61: public int getLineNumber() {
62: throw new Error("BufferReader.getLineNumber - not implemented");
63: }
64:
65: public int getColumnNumber() {
66: throw new Error(
67: "BufferReader.getColumnNumber - not implemented");
68: }
69:
70: }
|